{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Circuit Tracing Tutorial\n",
    "\n",
    "<a target=\"_blank\" href=\"https://colab.research.google.com/github/safety-research/circuit-tracer/blob/main/demos/circuit_tracing_tutorial.ipynb\">\n",
    "  <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
    "</a>\n",
    "\n",
    "This notebook serves as a tutorial for the circuit tracing library. The library enables users to explain model behavior by generating Attribution Graphs (introduced in [Circuit Tracing](https://transformer-circuits.pub/2025/attribution-graphs/methods.html)), and validate these explanations with experiments.\n",
    "\n",
    "If you'd like to generate your own graphs, you can do so on [Neuronpedia](https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-fact-dallas-austin&pruningThreshold=0.6&pinnedIds=27_22605_10%2C20_15589_10%2CE_26865_9%2C21_5943_10%2C23_12237_10%2C20_15589_9%2C16_25_9%2C14_2268_9%2C18_8959_10%2C4_13154_9%2C7_6861_9%2C19_1445_10%2CE_2329_7%2CE_6037_4%2C0_13727_7%2C6_4012_7%2C17_7178_10%2C15_4494_4%2C6_4662_4%2C4_7671_4%2C3_13984_4%2C1_1000_4%2C19_7477_9%2C18_6101_10%2C16_4298_10%2C7_691_10&supernodes=%5B%5B%22capital%22%2C%2215_4494_4%22%2C%226_4662_4%22%2C%224_7671_4%22%2C%223_13984_4%22%2C%221_1000_4%22%5D%2C%5B%22state%22%2C%226_4012_7%22%2C%220_13727_7%22%5D%2C%5B%22Texas%22%2C%2220_15589_9%22%2C%2219_7477_9%22%2C%2216_25_9%22%2C%224_13154_9%22%2C%2214_2268_9%22%2C%227_6861_9%22%5D%2C%5B%22preposition+followed+by+place+name%22%2C%2219_1445_10%22%2C%2218_6101_10%22%5D%2C%5B%22capital+cities+%2F+say+a+capital+city%22%2C%2221_5943_10%22%2C%2217_7178_10%22%2C%227_691_10%22%2C%2216_4298_10%22%5D%5D&densityThreshold=0.99&clerps=%5B%5B%2223_2312237_10%22%2C%22Cities+and+states+names+%28say+Austin%29%22%5D%2C%5B%2218_1808959_10%22%2C%22state+%2F+regional+government%22%5D%5D). You can also do so directly on Colab using this [starter notebook](https://github.com/safety-research/circuit-tracer/blob/main/demos/attribute_demo.ipynb).\n",
    "\n",
    "In this demo, we'll dive  into a couple of attribution graphs involving Gemma 2 (2B), based on the Multi-Step Reasoning and Multilingual Circuits examples [in the original paper](https://transformer-circuits.pub/2025/attribution-graphs/biology.html). We'll view the graphs, annotated with labeled supernodes, and then perform interventions to verify that the supernodes' labels are indeed correct, and that interventions have the effect we expect. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#@title Colab Setup Environment\n",
    "\n",
    "try:\n",
    "    import google.colab\n",
    "    !mkdir -p repository && cd repository && \\\n",
    "     git clone https://github.com/safety-research/circuit-tracer && \\\n",
    "     curl -LsSf https://astral.sh/uv/install.sh | sh && \\\n",
    "     uv pip install -e circuit-tracer/\n",
    "\n",
    "    import sys\n",
    "    from huggingface_hub import notebook_login\n",
    "    sys.path.append('repository/circuit-tracer')\n",
    "    sys.path.append('repository/circuit-tracer/demos')\n",
    "    notebook_login(new_session=False)\n",
    "    IN_COLAB = True\n",
    "except ImportError:\n",
    "    IN_COLAB = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "54bc8b14140c43839e207ff7be6f8c81",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Fetching 26 files:   0%|          | 0/26 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c0c90f25ffa54f14aebac4c580443275",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/3 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded pretrained model google/gemma-2-2b into HookedTransformer\n"
     ]
    }
   ],
   "source": [
    "from collections import namedtuple\n",
    "from typing import List, Dict\n",
    "\n",
    "import torch\n",
    "\n",
    "from circuit_tracer import ReplacementModel\n",
    "from circuit_tracer.utils.decode_url_features import decode_url_features\n",
    "\n",
    "model = ReplacementModel.from_pretrained(\"google/gemma-2-2b\", 'gemma', dtype=torch.bfloat16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Two-hop reasoning\n",
    "We'll start with the example [`Fact: The capital of the state containing Dallas is` → `Austin`](https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-fact-dallas-austin&pruningThreshold=0.6&pinnedIds=27_22605_10%2C20_15589_10%2CE_26865_9%2C21_5943_10%2C23_12237_10%2C20_15589_9%2C16_25_9%2C14_2268_9%2C18_8959_10%2C4_13154_9%2C7_6861_9%2C19_1445_10%2CE_2329_7%2CE_6037_4%2C0_13727_7%2C6_4012_7%2C17_7178_10%2C15_4494_4%2C6_4662_4%2C4_7671_4%2C3_13984_4%2C1_1000_4%2C19_7477_9%2C18_6101_10%2C16_4298_10%2C7_691_10&supernodes=%5B%5B%22capital%22%2C%2215_4494_4%22%2C%226_4662_4%22%2C%224_7671_4%22%2C%223_13984_4%22%2C%221_1000_4%22%5D%2C%5B%22state%22%2C%226_4012_7%22%2C%220_13727_7%22%5D%2C%5B%22Texas%22%2C%2220_15589_9%22%2C%2219_7477_9%22%2C%2216_25_9%22%2C%224_13154_9%22%2C%2214_2268_9%22%2C%227_6861_9%22%5D%2C%5B%22preposition+followed+by+place+name%22%2C%2219_1445_10%22%2C%2218_6101_10%22%5D%2C%5B%22capital+cities+%2F+say+a+capital+city%22%2C%2221_5943_10%22%2C%2217_7178_10%22%2C%227_691_10%22%2C%2216_4298_10%22%5D%5D&densityThreshold=0.99&clerps=%5B%5B%2223_2312237_10%22%2C%22Cities+and+states+names+%28say+Austin%29%22%5D%2C%5B%2218_1808959_10%22%2C%22state+%2F+regional+government%22%5D%5D), which requires two hop-reasoning. The model must first reason that the state containing Dallas is Texas; then, it must respond with the capital of Texas, Austin.\n",
    "\n",
    "[The original paper](https://transformer-circuits.pub/2025/attribution-graphs/biology.html#dives-tracing) showed that Haiku solves the problem using the following circuit, computing the intermediate step of the state containing Dallas: Texas.\n",
    "\n",
    "<img src=\"https://raw.githubusercontent.com/safety-research/circuit-tracer/main/demos/img/dallas-austin-haiku.png\" width=\"300\" />\n",
    "\n",
    "Performing attribution on Gemma 2 (2B) shows that it uses [this circuit](https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-fact-dallas-austin&pruningThreshold=0.6&pinnedIds=27_22605_10%2C20_15589_10%2CE_26865_9%2C21_5943_10%2C23_12237_10%2C20_15589_9%2C16_25_9%2C14_2268_9%2C18_8959_10%2C4_13154_9%2C7_6861_9%2C19_1445_10%2CE_2329_7%2CE_6037_4%2C0_13727_7%2C6_4012_7%2C17_7178_10%2C15_4494_4%2C6_4662_4%2C4_7671_4%2C3_13984_4%2C1_1000_4%2C19_7477_9%2C18_6101_10%2C16_4298_10%2C7_691_10&supernodes=%5B%5B%22capital%22%2C%2215_4494_4%22%2C%226_4662_4%22%2C%224_7671_4%22%2C%223_13984_4%22%2C%221_1000_4%22%5D%2C%5B%22state%22%2C%226_4012_7%22%2C%220_13727_7%22%5D%2C%5B%22Texas%22%2C%2220_15589_9%22%2C%2219_7477_9%22%2C%2216_25_9%22%2C%224_13154_9%22%2C%2214_2268_9%22%2C%227_6861_9%22%5D%2C%5B%22preposition+followed+by+place+name%22%2C%2219_1445_10%22%2C%2218_6101_10%22%5D%2C%5B%22capital+cities+%2F+say+a+capital+city%22%2C%2221_5943_10%22%2C%2217_7178_10%22%2C%227_691_10%22%2C%2216_4298_10%22%5D%5D&densityThreshold=0.99&clerps=%5B%5B%2223_2312237_10%22%2C%22Cities+and+states+names+%28say+Austin%29%22%5D%2C%5B%2218_1808959_10%22%2C%22state+%2F+regional+government%22%5D%5D) to successfully complete the prompt:\n",
    "\n",
    "<img src=\"https://raw.githubusercontent.com/safety-research/circuit-tracer/main/demos/img/gemma/dallas-austin-new.png\" width=\"600\" />\n",
    "\n",
    "The circuit is similar: it has a node corresponding to Texas, and shows both a direct path from Dallas to Austin as well as an indirect path going through Texas. The Attribution Graph suggests a hypothesis about model behavior, based on using transcoders to approximate the behavior of MLPs. We can verify that our understanding of model behavior is correct by performing interventions directly on the underlying model.\n",
    "\n",
    "We'll perform interventions on each of the supernodes shown. So, we will first get the supernodes from this graph. We have a convenience function that will map a circuit URL (and the supernodes stored therein), to a list of Feature objects; each Feature is a tuple of `(layer, position, feature_index)`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dallas_austin_url=\"https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-fact-dallas-austin&clerps=%5B%5D&pruningThreshold=0.53&pinnedIds=27_22605_10%2C20_15589_10%2CE_26865_9%2C21_5943_10%2C23_12237_10%2C20_15589_9%2C16_25_9%2C14_2268_9%2C18_8959_10%2C4_13154_9%2C7_6861_9%2C19_1445_10%2CE_2329_7%2CE_6037_4%2C0_13727_7%2C6_4012_7%2C17_7178_10%2C15_4494_4%2C6_4662_4%2C4_7671_4%2C3_13984_4%2C1_1000_4%2C19_7477_9%2C18_6101_10%2C16_4298_10%2C7_691_10&supernodes=%5B%5B%22capital%22%2C%2215_4494_4%22%2C%226_4662_4%22%2C%224_7671_4%22%2C%223_13984_4%22%2C%221_1000_4%22%5D%2C%5B%22state%22%2C%226_4012_7%22%2C%220_13727_7%22%5D%2C%5B%22Texas%22%2C%2220_15589_9%22%2C%2219_7477_9%22%2C%2216_25_9%22%2C%224_13154_9%22%2C%2214_2268_9%22%2C%227_6861_9%22%5D%2C%5B%22preposition+followed+by+place+name%22%2C%2219_1445_10%22%2C%2218_6101_10%22%5D%2C%5B%22capital+cities+%2F+say+a+capital+city%22%2C%2221_5943_10%22%2C%2217_7178_10%22%2C%227_691_10%22%2C%2216_4298_10%22%5D%5D\"\n",
    "supernode_features, _ = decode_url_features(dallas_austin_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll then create a representation of the circuit being used to solve this task. This means defining some Supernode objects, which will store a list of underlying features, as well as their children supernodes that they have a causal effect on."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from graph_visualization import create_graph_visualization, Supernode, InterventionGraph, Feature\n",
    "\n",
    "# Supernodes that upweight certain outputs. Note that e.g. the Say Austin node is not the only node promoting Austin, but that is its primary role\n",
    "say_austin_node = Supernode(name='Say Austin', features=[Feature(layer=23, pos=10, feature_idx=12237)])\n",
    "say_capital_node = Supernode(name='Say a capital', features=supernode_features['capital cities / say a capital city'], children=[say_austin_node])\n",
    "\n",
    "# Intermediate nodes\n",
    "texas_node = Supernode(name='Texas', features=supernode_features['Texas'], children=[say_austin_node])\n",
    "state_node = Supernode(name='state', features=supernode_features['state'], children=[say_capital_node, texas_node])\n",
    "capital_node = Supernode(name='capital', features=supernode_features['capital'], children=[say_capital_node])\n",
    "\n",
    "# Embedding nodes\n",
    "dallas_node = Supernode(name='Emb: Dallas', features=None, children=[texas_node])\n",
    "state_emb_node = Supernode(name='Emb: state', features=None, children=[state_node])\n",
    "capital_emb_node = Supernode(name='Emb: capital', features=None, children=[capital_node])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We then initialize an InterventionGraph, which stores all of our Supernodes, and keeps track of their state. We'll also get the model's logits and activations on this prompt. We then set each node's default activation (its activation on the original prompt, without interventions), and set its activation fraction. The activation fraction is the node's current activation / its default activation. Since the current and default activations are the same, each node is at 100%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = \"Fact: the capital of the state containing Dallas is\"\n",
    "ordered_nodes = [[capital_emb_node, state_emb_node],[capital_node, state_node, dallas_node],[say_capital_node, texas_node], [say_austin_node]]\n",
    "dallas_austin_graph = InterventionGraph(ordered_nodes=ordered_nodes, prompt=prompt)\n",
    "\n",
    "logits, dallas_activations = model.get_activations(prompt)\n",
    "\n",
    "# initialize each node, adding it to the intervention graph and recording its default activation\n",
    "for node in [capital_node, state_node, dallas_node, say_capital_node, texas_node, say_austin_node]:\n",
    "    dallas_austin_graph.initialize_node(node, dallas_activations)  \n",
    "\n",
    "# set each node's current activation to a percent of its default activation (here 100%)\n",
    "dallas_austin_graph.set_node_activation_fractions(dallas_activations)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll record the top-5 logits as well, and then visualize our graph!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"700\" height=\"400\">\n",
       "    <!-- Background -->\n",
       "    <rect width=\"700\" height=\"400\" fill=\"#f5f5f5\"/>\n",
       "    <rect x=\"20\" y=\"20\" width=\"660\" height=\"360\" fill=\"white\" stroke=\"none\" rx=\"12\"/>\n",
       "    \n",
       "    <!-- Title -->\n",
       "    <text x=\"40\" y=\"45\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"14\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"1px\">Graph &amp; Interventions</text>\n",
       "    \n",
       "    <!-- Graph area (moved up significantly) -->\n",
       "    <g transform=\"translate(50, 0)\">\n",
       "        <line x1=\"450.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 383.8093677492609,157.9364805205158 379.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"150.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 220.952452655745,164.36493806727375 216.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 233.8093677492609,157.9364805205158 229.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 370.952452655745,164.36493806727375 366.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"267.5\" x2=\"150.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"150.0,211.94444444444446 158.8093677492609,213.49203607607137 154.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"375.0\" y1=\"267.5\" x2=\"300.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,211.94444444444446 308.8093677492609,213.49203607607137 304.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"156.38888888888889\" x2=\"300.0\" y2=\"100.83333333333331\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,100.83333333333331 295.952452655745,108.8093825117182 291.1906322507391,102.38092496496024\" fill=\"#8B4513\"/>\n",
       "<line x1=\"375.0\" y1=\"156.38888888888889\" x2=\"300.0\" y2=\"100.83333333333331\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,100.83333333333331 308.8093677492609,102.38092496496024 304.047547344255,108.8093825117182\" fill=\"#8B4513\"/>\n",
       "        <rect x=\"175.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: capital</text>\n",
       "<rect x=\"325.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: state</text>\n",
       "<rect x=\"100.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"150.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">capital</text>\n",
       "<rect x=\"85.0\" y=\"189.44444444444446\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"100.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"250.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">state</text>\n",
       "<rect x=\"235.0\" y=\"189.44444444444446\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"400.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"450.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: Dallas</text>\n",
       "<rect x=\"175.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say a capital</text>\n",
       "<rect x=\"160.0\" y=\"133.88888888888889\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"175.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"325.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Texas</text>\n",
       "<rect x=\"310.0\" y=\"133.88888888888889\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"325.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"250.0\" y=\"83.33333333333331\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"105.33333333333331\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say Austin</text>\n",
       "<rect x=\"235.0\" y=\"78.33333333333331\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"90.33333333333331\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "    </g>\n",
       "    \n",
       "    <!-- Prompt section -->\n",
       "    <line x1=\"40\" y1=\"290\" x2=\"660\" y2=\"290\" stroke=\"#ddd\" stroke-width=\"1\"/>\n",
       "    <text x=\"40\" y=\"310\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Prompt</text>\n",
       "    \n",
       "    <!-- Prompt text (GitHub-compatible) -->\n",
       "    <text x=\"40\" y=\"325\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\">Fact: the capital of the state containing Dallas is</text>\n",
       "    \n",
       "    <!-- Top outputs section -->\n",
       "    <text x=\"40\" y=\"350\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Top Outputs</text>\n",
       "    \n",
       "    <!-- Output items -->\n",
       "    <g transform=\"translate(0, 5)\">\n",
       "        <rect x=\"40\" y=\"350\" width=\"94\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"45\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Austin <tspan fill=\"#555\" font-size=\"10\">42%</tspan></text>\n",
       "<rect x=\"144\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"149\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> not <tspan fill=\"#555\" font-size=\"10\">6%</tspan></text>\n",
       "<rect x=\"218\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"223\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> the <tspan fill=\"#555\" font-size=\"10\">6%</tspan></text>\n",
       "<rect x=\"292\" y=\"350\" width=\"80\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"297\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Texas <tspan fill=\"#555\" font-size=\"10\">5%</tspan></text>\n",
       "<rect x=\"382\" y=\"350\" width=\"72\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"387\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Fort <tspan fill=\"#555\" font-size=\"10\">4%</tspan></text>\n",
       "    </g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def get_top_outputs(logits: torch.Tensor, k: int = 5):\n",
    "    top_probs, top_token_ids = logits.squeeze(0)[-1].softmax(-1).topk(k)\n",
    "    top_tokens = [model.tokenizer.decode(token_id) for token_id in top_token_ids]\n",
    "    top_outputs = list(zip(top_tokens, top_probs.tolist()))\n",
    "    return top_outputs\n",
    "\n",
    "top_outputs = get_top_outputs(logits)\n",
    "\n",
    "create_graph_visualization(dallas_austin_graph, top_outputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The displayed circuit is consistent with the supernodes we created when visualizing the full graph. We'll now verify that each supernode plays the role we hypothesized using interventions. Each intervention will set a node's value to some multiple of its original value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# An Intervention says \"set the activation of supernode to intervention_value * its activation in the given activations tensor\"\n",
    "Intervention = namedtuple('Intervention', ['supernode', 'scaling_factor'])\n",
    "def supernode_intervention(intervention_graph: InterventionGraph, interventions: List[Intervention], replacements: Dict[str, Supernode] = None):\n",
    "    \"\"\"Performs interventions on a set of supernodes, records the outputs, and draws the corresponding graph\n",
    "\n",
    "    Args:\n",
    "        interventions (List[Intervention]): List of interventions to perform\n",
    "        replacements (Dict[str, Supernode], optional): Replacement supernodes to add, if we're adding supernodes \n",
    "            from another prompt. Defaults to None.\n",
    "\n",
    "    Returns:\n",
    "        HTML: An IPython.display.HTML object showing the graph corresponding to these interventions\n",
    "    \"\"\"\n",
    "    intervention_values = [(*feature, scaling_factor * default_act) for intervened_supernode, scaling_factor in interventions \n",
    "                           for feature, default_act in zip(intervened_supernode.features, intervened_supernode.default_activations)]\n",
    "    new_logits, new_activations = model.feature_intervention(intervention_graph.prompt, intervention_values)\n",
    "    intervention_graph.set_node_activation_fractions(new_activations)\n",
    "    top_outputs = get_top_outputs(new_logits)\n",
    "\n",
    "    for intervened_supernode, scaling_factor in interventions:\n",
    "        intervened_supernode.activation = None\n",
    "        intervened_supernode.intervention = f'{scaling_factor}x'\n",
    "\n",
    "    if replacements is not None:\n",
    "        for target, replacement in replacements.items():\n",
    "            intervention_graph.nodes[target].replacement_node = replacement\n",
    "\n",
    "    return create_graph_visualization(intervention_graph, top_outputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the original paper, turning off the \"Say a capital\" feature caused the \"Say Austin\" supernode to shut off, and the model's top logit to change to Texas. What happens if we do the same?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"700\" height=\"400\">\n",
       "    <!-- Background -->\n",
       "    <rect width=\"700\" height=\"400\" fill=\"#f5f5f5\"/>\n",
       "    <rect x=\"20\" y=\"20\" width=\"660\" height=\"360\" fill=\"white\" stroke=\"none\" rx=\"12\"/>\n",
       "    \n",
       "    <!-- Title -->\n",
       "    <text x=\"40\" y=\"45\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"14\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"1px\">Graph &amp; Interventions</text>\n",
       "    \n",
       "    <!-- Graph area (moved up significantly) -->\n",
       "    <g transform=\"translate(50, 0)\">\n",
       "        <line x1=\"450.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 383.8093677492609,157.9364805205158 379.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"150.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 220.952452655745,164.36493806727375 216.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 233.8093677492609,157.9364805205158 229.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 370.952452655745,164.36493806727375 366.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"267.5\" x2=\"150.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"150.0,211.94444444444446 158.8093677492609,213.49203607607137 154.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"375.0\" y1=\"267.5\" x2=\"300.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,211.94444444444446 308.8093677492609,213.49203607607137 304.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"156.38888888888889\" x2=\"300.0\" y2=\"100.83333333333331\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,100.83333333333331 295.952452655745,108.8093825117182 291.1906322507391,102.38092496496024\" fill=\"#8B4513\"/>\n",
       "<line x1=\"375.0\" y1=\"156.38888888888889\" x2=\"300.0\" y2=\"100.83333333333331\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,100.83333333333331 308.8093677492609,102.38092496496024 304.047547344255,108.8093825117182\" fill=\"#8B4513\"/>\n",
       "        <rect x=\"175.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: capital</text>\n",
       "<rect x=\"325.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: state</text>\n",
       "<rect x=\"100.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"150.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">capital</text>\n",
       "<rect x=\"85.0\" y=\"189.44444444444446\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"100.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"250.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">state</text>\n",
       "<rect x=\"235.0\" y=\"189.44444444444446\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"400.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"450.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: Dallas</text>\n",
       "<rect x=\"175.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say a capital</text>\n",
       "<rect x=\"155.0\" y=\"133.88888888888889\" width=\"34\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"172.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">-2x</text>\n",
       "<rect x=\"325.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Texas</text>\n",
       "<rect x=\"310.0\" y=\"133.88888888888889\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"325.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"250.0\" y=\"83.33333333333331\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"105.33333333333331\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say Austin</text>\n",
       "<rect x=\"235.0\" y=\"78.33333333333331\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"90.33333333333331\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">0%</text>\n",
       "    </g>\n",
       "    \n",
       "    <!-- Prompt section -->\n",
       "    <line x1=\"40\" y1=\"290\" x2=\"660\" y2=\"290\" stroke=\"#ddd\" stroke-width=\"1\"/>\n",
       "    <text x=\"40\" y=\"310\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Prompt</text>\n",
       "    \n",
       "    <!-- Prompt text (GitHub-compatible) -->\n",
       "    <text x=\"40\" y=\"325\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\">Fact: the capital of the state containing Dallas is</text>\n",
       "    \n",
       "    <!-- Top outputs section -->\n",
       "    <text x=\"40\" y=\"350\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Top Outputs</text>\n",
       "    \n",
       "    <!-- Output items -->\n",
       "    <g transform=\"translate(0, 5)\">\n",
       "        <rect x=\"40\" y=\"350\" width=\"86\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"45\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Texas <tspan fill=\"#555\" font-size=\"10\">46%</tspan></text>\n",
       "<rect x=\"136\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"141\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> the <tspan fill=\"#555\" font-size=\"10\">8%</tspan></text>\n",
       "<rect x=\"210\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"215\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> not <tspan fill=\"#555\" font-size=\"10\">5%</tspan></text>\n",
       "<rect x=\"284\" y=\"350\" width=\"88\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"289\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> called <tspan fill=\"#555\" font-size=\"10\">4%</tspan></text>\n",
       "<rect x=\"382\" y=\"350\" width=\"80\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"387\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> known <tspan fill=\"#555\" font-size=\"10\">3%</tspan></text>\n",
       "    </g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "supernode_intervention(dallas_austin_graph, [Intervention(say_capital_node, -2)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We observe precisely the same behavior! Strongly shutting off the \"Say a capital\" supernode turned off the \"Say Austin\" node, and changed the top logit to Texas. \n",
    "\n",
    "What if we turn off the \"capital\" supernode?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"700\" height=\"400\">\n",
       "    <!-- Background -->\n",
       "    <rect width=\"700\" height=\"400\" fill=\"#f5f5f5\"/>\n",
       "    <rect x=\"20\" y=\"20\" width=\"660\" height=\"360\" fill=\"white\" stroke=\"none\" rx=\"12\"/>\n",
       "    \n",
       "    <!-- Title -->\n",
       "    <text x=\"40\" y=\"45\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"14\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"1px\">Graph &amp; Interventions</text>\n",
       "    \n",
       "    <!-- Graph area (moved up significantly) -->\n",
       "    <g transform=\"translate(50, 0)\">\n",
       "        <line x1=\"450.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 383.8093677492609,157.9364805205158 379.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"150.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 220.952452655745,164.36493806727375 216.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 233.8093677492609,157.9364805205158 229.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 370.952452655745,164.36493806727375 366.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"267.5\" x2=\"150.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"150.0,211.94444444444446 158.8093677492609,213.49203607607137 154.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"375.0\" y1=\"267.5\" x2=\"300.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,211.94444444444446 308.8093677492609,213.49203607607137 304.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"156.38888888888889\" x2=\"300.0\" y2=\"100.83333333333331\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,100.83333333333331 295.952452655745,108.8093825117182 291.1906322507391,102.38092496496024\" fill=\"#8B4513\"/>\n",
       "<line x1=\"375.0\" y1=\"156.38888888888889\" x2=\"300.0\" y2=\"100.83333333333331\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,100.83333333333331 308.8093677492609,102.38092496496024 304.047547344255,108.8093825117182\" fill=\"#8B4513\"/>\n",
       "        <rect x=\"175.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: capital</text>\n",
       "<rect x=\"325.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: state</text>\n",
       "<rect x=\"100.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"150.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">capital</text>\n",
       "<rect x=\"80.0\" y=\"189.44444444444446\" width=\"34\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"97.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">-2x</text>\n",
       "<rect x=\"250.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">state</text>\n",
       "<rect x=\"235.0\" y=\"189.44444444444446\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">92%</text>\n",
       "<rect x=\"400.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"450.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: Dallas</text>\n",
       "<rect x=\"175.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say a capital</text>\n",
       "<rect x=\"160.0\" y=\"133.88888888888889\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"175.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">22%</text>\n",
       "<rect x=\"325.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Texas</text>\n",
       "<rect x=\"310.0\" y=\"133.88888888888889\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"325.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">98%</text>\n",
       "<rect x=\"250.0\" y=\"83.33333333333331\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"105.33333333333331\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say Austin</text>\n",
       "<rect x=\"235.0\" y=\"78.33333333333331\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"90.33333333333331\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">57%</text>\n",
       "    </g>\n",
       "    \n",
       "    <!-- Prompt section -->\n",
       "    <line x1=\"40\" y1=\"290\" x2=\"660\" y2=\"290\" stroke=\"#ddd\" stroke-width=\"1\"/>\n",
       "    <text x=\"40\" y=\"310\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Prompt</text>\n",
       "    \n",
       "    <!-- Prompt text (GitHub-compatible) -->\n",
       "    <text x=\"40\" y=\"325\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\">Fact: the capital of the state containing Dallas is</text>\n",
       "    \n",
       "    <!-- Top outputs section -->\n",
       "    <text x=\"40\" y=\"350\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Top Outputs</text>\n",
       "    \n",
       "    <!-- Output items -->\n",
       "    <g transform=\"translate(0, 5)\">\n",
       "        <rect x=\"40\" y=\"350\" width=\"86\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"45\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Texas <tspan fill=\"#555\" font-size=\"10\">28%</tspan></text>\n",
       "<rect x=\"136\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"141\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> the <tspan fill=\"#555\" font-size=\"10\">9%</tspan></text>\n",
       "<rect x=\"210\" y=\"350\" width=\"88\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"215\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> called <tspan fill=\"#555\" font-size=\"10\">7%</tspan></text>\n",
       "<rect x=\"308\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"313\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> not <tspan fill=\"#555\" font-size=\"10\">4%</tspan></text>\n",
       "<rect x=\"382\" y=\"350\" width=\"72\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"387\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Fort <tspan fill=\"#555\" font-size=\"10\">3%</tspan></text>\n",
       "    </g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "supernode_intervention(dallas_austin_graph, [Intervention(capital_node, -2)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This yields similar behavior to before. We turn off the \"Say a capital\" supernode, though not as strongly in as in the prior intervention. This also partially turns off the \"Say Austin\" node.\n",
    "\n",
    "What if we turn off the Texas supernode?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"700\" height=\"400\">\n",
       "    <!-- Background -->\n",
       "    <rect width=\"700\" height=\"400\" fill=\"#f5f5f5\"/>\n",
       "    <rect x=\"20\" y=\"20\" width=\"660\" height=\"360\" fill=\"white\" stroke=\"none\" rx=\"12\"/>\n",
       "    \n",
       "    <!-- Title -->\n",
       "    <text x=\"40\" y=\"45\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"14\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"1px\">Graph &amp; Interventions</text>\n",
       "    \n",
       "    <!-- Graph area (moved up significantly) -->\n",
       "    <g transform=\"translate(50, 0)\">\n",
       "        <line x1=\"450.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 383.8093677492609,157.9364805205158 379.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"150.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 220.952452655745,164.36493806727375 216.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 233.8093677492609,157.9364805205158 229.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 370.952452655745,164.36493806727375 366.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"267.5\" x2=\"150.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"150.0,211.94444444444446 158.8093677492609,213.49203607607137 154.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"375.0\" y1=\"267.5\" x2=\"300.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,211.94444444444446 308.8093677492609,213.49203607607137 304.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"156.38888888888889\" x2=\"300.0\" y2=\"100.83333333333331\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,100.83333333333331 295.952452655745,108.8093825117182 291.1906322507391,102.38092496496024\" fill=\"#8B4513\"/>\n",
       "<line x1=\"375.0\" y1=\"156.38888888888889\" x2=\"300.0\" y2=\"100.83333333333331\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,100.83333333333331 308.8093677492609,102.38092496496024 304.047547344255,108.8093825117182\" fill=\"#8B4513\"/>\n",
       "        <rect x=\"175.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: capital</text>\n",
       "<rect x=\"325.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: state</text>\n",
       "<rect x=\"100.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"150.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">capital</text>\n",
       "<rect x=\"85.0\" y=\"189.44444444444446\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"100.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"250.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">state</text>\n",
       "<rect x=\"235.0\" y=\"189.44444444444446\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"400.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"450.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: Dallas</text>\n",
       "<rect x=\"175.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say a capital</text>\n",
       "<rect x=\"160.0\" y=\"133.88888888888889\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"175.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">102%</text>\n",
       "<rect x=\"325.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Texas</text>\n",
       "<rect x=\"305.0\" y=\"133.88888888888889\" width=\"34\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"322.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">-2x</text>\n",
       "<rect x=\"250.0\" y=\"83.33333333333331\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"105.33333333333331\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say Austin</text>\n",
       "<rect x=\"235.0\" y=\"78.33333333333331\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"90.33333333333331\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">28%</text>\n",
       "    </g>\n",
       "    \n",
       "    <!-- Prompt section -->\n",
       "    <line x1=\"40\" y1=\"290\" x2=\"660\" y2=\"290\" stroke=\"#ddd\" stroke-width=\"1\"/>\n",
       "    <text x=\"40\" y=\"310\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Prompt</text>\n",
       "    \n",
       "    <!-- Prompt text (GitHub-compatible) -->\n",
       "    <text x=\"40\" y=\"325\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\">Fact: the capital of the state containing Dallas is</text>\n",
       "    \n",
       "    <!-- Top outputs section -->\n",
       "    <text x=\"40\" y=\"350\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Top Outputs</text>\n",
       "    \n",
       "    <!-- Output items -->\n",
       "    <g transform=\"translate(0, 5)\">\n",
       "        <rect x=\"40\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"45\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> the <tspan fill=\"#555\" font-size=\"10\">8%</tspan></text>\n",
       "<rect x=\"114\" y=\"350\" width=\"120\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"119\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Harrisburg <tspan fill=\"#555\" font-size=\"10\">7%</tspan></text>\n",
       "<rect x=\"244\" y=\"350\" width=\"88\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"249\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Albany <tspan fill=\"#555\" font-size=\"10\">7%</tspan></text>\n",
       "<rect x=\"342\" y=\"350\" width=\"104\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"347\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Hartford <tspan fill=\"#555\" font-size=\"10\">5%</tspan></text>\n",
       "<rect x=\"456\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"461\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> not <tspan fill=\"#555\" font-size=\"10\">5%</tspan></text>\n",
       "    </g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "supernode_intervention(dallas_austin_graph, [Intervention(texas_node, -2)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Turning the Texas supernode off also disabled the \"Say Austin\" node, yielding the capitals of other states.\n",
    "\n",
    "What if we turn off the \"state\" supernode?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"700\" height=\"400\">\n",
       "    <!-- Background -->\n",
       "    <rect width=\"700\" height=\"400\" fill=\"#f5f5f5\"/>\n",
       "    <rect x=\"20\" y=\"20\" width=\"660\" height=\"360\" fill=\"white\" stroke=\"none\" rx=\"12\"/>\n",
       "    \n",
       "    <!-- Title -->\n",
       "    <text x=\"40\" y=\"45\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"14\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"1px\">Graph &amp; Interventions</text>\n",
       "    \n",
       "    <!-- Graph area (moved up significantly) -->\n",
       "    <g transform=\"translate(50, 0)\">\n",
       "        <line x1=\"450.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 383.8093677492609,157.9364805205158 379.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"150.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 220.952452655745,164.36493806727375 216.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 233.8093677492609,157.9364805205158 229.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 370.952452655745,164.36493806727375 366.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"267.5\" x2=\"150.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"150.0,211.94444444444446 158.8093677492609,213.49203607607137 154.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"375.0\" y1=\"267.5\" x2=\"300.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,211.94444444444446 308.8093677492609,213.49203607607137 304.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"156.38888888888889\" x2=\"300.0\" y2=\"100.83333333333331\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,100.83333333333331 295.952452655745,108.8093825117182 291.1906322507391,102.38092496496024\" fill=\"#8B4513\"/>\n",
       "<line x1=\"375.0\" y1=\"156.38888888888889\" x2=\"300.0\" y2=\"100.83333333333331\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,100.83333333333331 308.8093677492609,102.38092496496024 304.047547344255,108.8093825117182\" fill=\"#8B4513\"/>\n",
       "        <rect x=\"175.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: capital</text>\n",
       "<rect x=\"325.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: state</text>\n",
       "<rect x=\"100.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"150.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">capital</text>\n",
       "<rect x=\"85.0\" y=\"189.44444444444446\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"100.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"250.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">state</text>\n",
       "<rect x=\"230.0\" y=\"189.44444444444446\" width=\"34\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"247.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">-2x</text>\n",
       "<rect x=\"400.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"450.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: Dallas</text>\n",
       "<rect x=\"175.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say a capital</text>\n",
       "<rect x=\"160.0\" y=\"133.88888888888889\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"175.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">99%</text>\n",
       "<rect x=\"325.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Texas</text>\n",
       "<rect x=\"310.0\" y=\"133.88888888888889\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"325.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">97%</text>\n",
       "<rect x=\"250.0\" y=\"83.33333333333331\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"105.33333333333331\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say Austin</text>\n",
       "<rect x=\"235.0\" y=\"78.33333333333331\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"90.33333333333331\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "    </g>\n",
       "    \n",
       "    <!-- Prompt section -->\n",
       "    <line x1=\"40\" y1=\"290\" x2=\"660\" y2=\"290\" stroke=\"#ddd\" stroke-width=\"1\"/>\n",
       "    <text x=\"40\" y=\"310\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Prompt</text>\n",
       "    \n",
       "    <!-- Prompt text (GitHub-compatible) -->\n",
       "    <text x=\"40\" y=\"325\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\">Fact: the capital of the state containing Dallas is</text>\n",
       "    \n",
       "    <!-- Top outputs section -->\n",
       "    <text x=\"40\" y=\"350\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Top Outputs</text>\n",
       "    \n",
       "    <!-- Output items -->\n",
       "    <g transform=\"translate(0, 5)\">\n",
       "        <rect x=\"40\" y=\"350\" width=\"94\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"45\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Austin <tspan fill=\"#555\" font-size=\"10\">38%</tspan></text>\n",
       "<rect x=\"144\" y=\"350\" width=\"80\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"149\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Texas <tspan fill=\"#555\" font-size=\"10\">7%</tspan></text>\n",
       "<rect x=\"234\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"239\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> the <tspan fill=\"#555\" font-size=\"10\">6%</tspan></text>\n",
       "<rect x=\"308\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"313\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> not <tspan fill=\"#555\" font-size=\"10\">5%</tspan></text>\n",
       "<rect x=\"382\" y=\"350\" width=\"72\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"387\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Fort <tspan fill=\"#555\" font-size=\"10\">3%</tspan></text>\n",
       "    </g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "supernode_intervention(dallas_austin_graph, [Intervention(state_node, -2)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Turning off the state supernode was largely ineffective: it caused little change to any of the other supernode activations, and little change to the model logits.\n",
    "\n",
    "We’ve validated the behavior of nodes by ablating them. Could we inject entirely different nodes and validate that they have the expected effect? Take the circuit from the prompt [`Fact: The capital of the state containing Oakland is` → `Sacramento`]((https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-fact-oakland-sacramento&pruningThreshold=0.5&pinnedIds=27_43939_10%2CE_49024_9%2C21_5943_10%2C19_9209_10%2C18_8959_10%2C14_12562_9%2C7_14530_9%2C8_14641_9%2C4_8625_9%2C19_9209_9%2C17_7178_10%2CE_6037_4%2C15_4494_4%2CE_2329_7%2C16_4298_10%2C7_691_10%2C6_4662_4%2C4_7671_4%2C2_8734_4%2C0_1961_4%2C6_13909_9%2C22_4367_10%2C21_2464_10&supernodes=%5B%5B%22capital%22%2C%226_4662_4%22%2C%2215_4494_4%22%2C%224_7671_4%22%2C%222_8734_4%22%2C%220_1961_4%22%5D%2C%5B%22capital%22%2C%2221_5943_10%22%2C%2216_4298_10%22%2C%2217_7178_10%22%2C%227_691_10%22%5D%2C%5B%22California%22%2C%2222_4367_10%22%2C%2221_2464_10%22%5D%2C%5B%22California%22%2C%226_13909_9%22%2C%228_14641_9%22%2C%2214_12562_9%22%5D%2C%5B%22Bay+Area+%28say+California%29%22%2C%2219_9209_9%22%2C%227_14530_9%22%2C%224_8625_9%22%5D%5D&clickedId=27_43939_10&densityThreshold=0.99&clerps=%5B%5B%2219_1909209_9%22%2C%22Bay+area%22%5D%2C%5B%2219_1909209_10%22%2C%22Bay+Area%22%5D%5D)). We'll add two supernodes from this graph—\"California\" and \"Say Sacramento\" to our InterventionGraph."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "oakland_prompt = \"Fact: the capital of the state containing Oakland is\"\n",
    "_, oakland_activations = model.get_activations(oakland_prompt)\n",
    "\n",
    "oakland_url = \"https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-fact-oakland-sacramento&clerps=%5B%5D&pruningThreshold=0.5&pinnedIds=27_43939_10%2CE_49024_9%2C21_5943_10%2C19_9209_10%2C18_8959_10%2C14_12562_9%2C7_14530_9%2C8_14641_9%2C4_8625_9%2C19_9209_9%2C17_7178_10%2CE_6037_4%2C15_4494_4%2CE_2329_7%2C16_4298_10%2C7_691_10%2C6_4662_4%2C4_7671_4%2C2_8734_4%2C0_1961_4%2C6_13909_9%2C22_4367_10%2C21_2464_10&supernodes=%5B%5B%22capital%22%2C%226_4662_4%22%2C%2215_4494_4%22%2C%224_7671_4%22%2C%222_8734_4%22%2C%220_1961_4%22%5D%2C%5B%22capital%22%2C%2221_5943_10%22%2C%2216_4298_10%22%2C%2217_7178_10%22%2C%227_691_10%22%5D%2C%5B%22California%22%2C%2222_4367_10%22%2C%2221_2464_10%22%5D%2C%5B%22Bay+Area+%28say+California%29%22%2C%227_14530_9%22%2C%224_8625_9%22%5D%2C%5B%22California%22%2C%226_13909_9%22%2C%228_14641_9%22%2C%2214_12562_9%22%5D%5D&clickedId=27_43939_10\"\n",
    "oakland_supernodes, _ = decode_url_features(oakland_url)\n",
    "\n",
    "say_sacramento_node = Supernode('Say Sacramento', features=[Feature(layer=19, pos=10, feature_idx=9209)])\n",
    "california_node = Supernode('California', features=oakland_supernodes['California'] + oakland_supernodes['California (2)'], \n",
    "                       children=[say_sacramento_node])\n",
    "\n",
    "for node in [say_sacramento_node, california_node]:\n",
    "    dallas_austin_graph.initialize_node(node, oakland_activations)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then, we'll perform an intervention, turning off the Texas supernode, and turning on the California one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"700\" height=\"400\">\n",
       "    <!-- Background -->\n",
       "    <rect width=\"700\" height=\"400\" fill=\"#f5f5f5\"/>\n",
       "    <rect x=\"20\" y=\"20\" width=\"660\" height=\"360\" fill=\"white\" stroke=\"none\" rx=\"12\"/>\n",
       "    \n",
       "    <!-- Title -->\n",
       "    <text x=\"40\" y=\"45\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"14\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"1px\">Graph &amp; Interventions</text>\n",
       "    \n",
       "    <!-- Graph area (moved up significantly) -->\n",
       "    <g transform=\"translate(50, 0)\">\n",
       "        <line x1=\"450.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 383.8093677492609,157.9364805205158 379.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"150.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 220.952452655745,164.36493806727375 216.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"405.0\" y1=\"121.38888888888889\" x2=\"330.0\" y2=\"65.83333333333331\" stroke=\"#D2691E\" stroke-width=\"4\"/>\n",
       "<polygon points=\"330.0,65.83333333333331 338.8093677492609,67.38092496496024 334.047547344255,73.8093825117182\" fill=\"#D2691E\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 233.8093677492609,157.9364805205158 229.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 370.952452655745,164.36493806727375 366.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"267.5\" x2=\"150.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"150.0,211.94444444444446 158.8093677492609,213.49203607607137 154.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"375.0\" y1=\"267.5\" x2=\"300.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,211.94444444444446 308.8093677492609,213.49203607607137 304.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"156.38888888888889\" x2=\"300.0\" y2=\"100.83333333333331\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,100.83333333333331 295.952452655745,108.8093825117182 291.1906322507391,102.38092496496024\" fill=\"#8B4513\"/>\n",
       "        <rect x=\"175.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: capital</text>\n",
       "<rect x=\"325.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: state</text>\n",
       "<rect x=\"100.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"150.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">capital</text>\n",
       "<rect x=\"85.0\" y=\"189.44444444444446\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"100.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"250.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">state</text>\n",
       "<rect x=\"235.0\" y=\"189.44444444444446\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"400.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"450.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: Dallas</text>\n",
       "<rect x=\"175.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say a capital</text>\n",
       "<rect x=\"160.0\" y=\"133.88888888888889\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"175.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">101%</text>\n",
       "<rect x=\"325.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Texas</text>\n",
       "<rect x=\"305.0\" y=\"133.88888888888889\" width=\"34\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"322.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">-2x</text>\n",
       "<rect x=\"250.0\" y=\"83.33333333333331\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"105.33333333333331\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say Austin</text>\n",
       "<rect x=\"235.0\" y=\"78.33333333333331\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"90.33333333333331\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">0%</text>\n",
       "<rect x=\"280.0\" y=\"48.333333333333314\" width=\"100\" height=\"35\" fill=\"#FFF8DC\" stroke=\"#D2691E\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"330.0\" y=\"70.33333333333331\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say Sacramento</text>\n",
       "<rect x=\"265.0\" y=\"43.333333333333314\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"280.0\" y=\"55.333333333333314\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">27%</text>\n",
       "<rect x=\"355.0\" y=\"103.88888888888889\" width=\"100\" height=\"35\" fill=\"#FFF8DC\" stroke=\"#D2691E\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"405.0\" y=\"125.88888888888889\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">California</text>\n",
       "<rect x=\"335.0\" y=\"98.88888888888889\" width=\"26\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"348.0\" y=\"110.88888888888889\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">2x</text>\n",
       "    </g>\n",
       "    \n",
       "    <!-- Prompt section -->\n",
       "    <line x1=\"40\" y1=\"290\" x2=\"660\" y2=\"290\" stroke=\"#ddd\" stroke-width=\"1\"/>\n",
       "    <text x=\"40\" y=\"310\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Prompt</text>\n",
       "    \n",
       "    <!-- Prompt text (GitHub-compatible) -->\n",
       "    <text x=\"40\" y=\"325\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\">Fact: the capital of the state containing Dallas is</text>\n",
       "    \n",
       "    <!-- Top outputs section -->\n",
       "    <text x=\"40\" y=\"350\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Top Outputs</text>\n",
       "    \n",
       "    <!-- Output items -->\n",
       "    <g transform=\"translate(0, 5)\">\n",
       "        <rect x=\"40\" y=\"350\" width=\"126\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"45\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Sacramento <tspan fill=\"#555\" font-size=\"10\">34%</tspan></text>\n",
       "<rect x=\"176\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"181\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> the <tspan fill=\"#555\" font-size=\"10\">7%</tspan></text>\n",
       "<rect x=\"250\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"255\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> not <tspan fill=\"#555\" font-size=\"10\">5%</tspan></text>\n",
       "<rect x=\"324\" y=\"350\" width=\"40\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"329\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">  <tspan fill=\"#555\" font-size=\"10\">2%</tspan></text>\n",
       "<rect x=\"374\" y=\"350\" width=\"96\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"379\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Olympia <tspan fill=\"#555\" font-size=\"10\">2%</tspan></text>\n",
       "    </g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oakland_interventions = [Intervention(texas_node, -2), Intervention(california_node, 2)]\n",
    "supernode_intervention(dallas_austin_graph, oakland_interventions, {texas_node.name: california_node, say_austin_node.name: say_sacramento_node})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Doing this caused the \"Say Austin\" node to shut off entirely, while the \"Say Sacramento\" node began to activate! Our model's top output is now Sacramento, as well.\n",
    "\n",
    "We can also do this replacing states with countries. Take the circuit for [`Fact: The capital of the country containing Shanghai is` → `Beijing`](https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-fact-shanghai-beijing&clerps=%5B%5D&clickedId=15_4494_4&pruningThreshold=0.45&pinnedIds=27_33395_10%2CE_38628_9%2C21_5943_10%2C19_12274_10%2C19_12274_9%2C14_12274_9%2C18_6101_10%2C17_7178_10%2C6_6811_9%2C4_4257_9%2C4_11570_9%2CE_6037_4%2C0_8885_4%2C18_7639_10%2C19_2695_10%2C16_4298_10%2C15_4494_4%2C6_4662_4&supernodes=%5B%5B%22China%22%2C%2219_12274_9%22%2C%2214_12274_9%22%2C%226_6811_9%22%2C%224_11570_9%22%2C%224_4257_9%22%5D%2C%5B%22China%22%2C%2219_12274_10%22%2C%2218_7639_10%22%5D%2C%5B%22capital%22%2C%2216_4298_10%22%2C%2217_7178_10%22%2C%2218_6101_10%22%2C%2219_2695_10%22%2C%2221_5943_10%22%5D%2C%5B%22capital+cities+%28say+city%29%22%2C%226_4662_4%22%2C%2215_4494_4%22%2C%220_8885_4%22%5D%5D). We'll do precisely what we did before, disabling Texas and activating the China supernode. This time, there is no \"Say Beijing\" node, but the effect of this intervention should be visible in the logits."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"700\" height=\"400\">\n",
       "    <!-- Background -->\n",
       "    <rect width=\"700\" height=\"400\" fill=\"#f5f5f5\"/>\n",
       "    <rect x=\"20\" y=\"20\" width=\"660\" height=\"360\" fill=\"white\" stroke=\"none\" rx=\"12\"/>\n",
       "    \n",
       "    <!-- Title -->\n",
       "    <text x=\"40\" y=\"45\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"14\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"1px\">Graph &amp; Interventions</text>\n",
       "    \n",
       "    <!-- Graph area (moved up significantly) -->\n",
       "    <g transform=\"translate(50, 0)\">\n",
       "        <line x1=\"450.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 383.8093677492609,157.9364805205158 379.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"150.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 220.952452655745,164.36493806727375 216.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 233.8093677492609,157.9364805205158 229.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 370.952452655745,164.36493806727375 366.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"267.5\" x2=\"150.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"150.0,211.94444444444446 158.8093677492609,213.49203607607137 154.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"375.0\" y1=\"267.5\" x2=\"300.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,211.94444444444446 308.8093677492609,213.49203607607137 304.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"156.38888888888889\" x2=\"300.0\" y2=\"100.83333333333331\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,100.83333333333331 295.952452655745,108.8093825117182 291.1906322507391,102.38092496496024\" fill=\"#8B4513\"/>\n",
       "        <rect x=\"175.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: capital</text>\n",
       "<rect x=\"325.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: state</text>\n",
       "<rect x=\"100.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"150.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">capital</text>\n",
       "<rect x=\"85.0\" y=\"189.44444444444446\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"100.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"250.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">state</text>\n",
       "<rect x=\"235.0\" y=\"189.44444444444446\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"400.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"450.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: Dallas</text>\n",
       "<rect x=\"175.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say a capital</text>\n",
       "<rect x=\"160.0\" y=\"133.88888888888889\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"175.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">103%</text>\n",
       "<rect x=\"325.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Texas</text>\n",
       "<rect x=\"305.0\" y=\"133.88888888888889\" width=\"34\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"322.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">-2x</text>\n",
       "<rect x=\"250.0\" y=\"83.33333333333331\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"105.33333333333331\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say Austin</text>\n",
       "<rect x=\"235.0\" y=\"78.33333333333331\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"90.33333333333331\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">30%</text>\n",
       "<rect x=\"355.0\" y=\"103.88888888888889\" width=\"100\" height=\"35\" fill=\"#FFF8DC\" stroke=\"#D2691E\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"405.0\" y=\"125.88888888888889\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">China</text>\n",
       "<rect x=\"335.0\" y=\"98.88888888888889\" width=\"26\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"348.0\" y=\"110.88888888888889\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">2x</text>\n",
       "    </g>\n",
       "    \n",
       "    <!-- Prompt section -->\n",
       "    <line x1=\"40\" y1=\"290\" x2=\"660\" y2=\"290\" stroke=\"#ddd\" stroke-width=\"1\"/>\n",
       "    <text x=\"40\" y=\"310\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Prompt</text>\n",
       "    \n",
       "    <!-- Prompt text (GitHub-compatible) -->\n",
       "    <text x=\"40\" y=\"325\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\">Fact: the capital of the state containing Dallas is</text>\n",
       "    \n",
       "    <!-- Top outputs section -->\n",
       "    <text x=\"40\" y=\"350\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Top Outputs</text>\n",
       "    \n",
       "    <!-- Output items -->\n",
       "    <g transform=\"translate(0, 5)\">\n",
       "        <rect x=\"40\" y=\"350\" width=\"102\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"45\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Beijing <tspan fill=\"#555\" font-size=\"10\">22%</tspan></text>\n",
       "<rect x=\"152\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"157\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> not <tspan fill=\"#555\" font-size=\"10\">7%</tspan></text>\n",
       "<rect x=\"226\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"231\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> the <tspan fill=\"#555\" font-size=\"10\">7%</tspan></text>\n",
       "<rect x=\"300\" y=\"350\" width=\"96\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"305\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Nanjing <tspan fill=\"#555\" font-size=\"10\">5%</tspan></text>\n",
       "<rect x=\"406\" y=\"350\" width=\"104\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"411\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Shanghai <tspan fill=\"#555\" font-size=\"10\">4%</tspan></text>\n",
       "    </g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shanghai_prompt = \"Fact: the capital of the country containing Shanghai is\"\n",
    "_, shanghai_activations = model.get_activations(shanghai_prompt)\n",
    "\n",
    "shanghai_url = \"https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-fact-shanghai-beijing&clerps=%5B%5D&clickedId=15_4494_4&pruningThreshold=0.45&pinnedIds=27_33395_10%2CE_38628_9%2C21_5943_10%2C19_12274_10%2C19_12274_9%2C14_12274_9%2C18_6101_10%2C17_7178_10%2C6_6811_9%2C4_4257_9%2C4_11570_9%2CE_6037_4%2C0_8885_4%2C18_7639_10%2C19_2695_10%2C16_4298_10%2C15_4494_4%2C6_4662_4&supernodes=%5B%5B%22China%22%2C%2219_12274_9%22%2C%2214_12274_9%22%2C%226_6811_9%22%2C%224_11570_9%22%2C%224_4257_9%22%5D%2C%5B%22China%22%2C%2219_12274_10%22%2C%2218_7639_10%22%5D%2C%5B%22capital%22%2C%2216_4298_10%22%2C%2217_7178_10%22%2C%2218_6101_10%22%2C%2219_2695_10%22%2C%2221_5943_10%22%5D%2C%5B%22capital+cities+%28say+city%29%22%2C%226_4662_4%22%2C%2215_4494_4%22%2C%220_8885_4%22%5D%5D\"\n",
    "shanghai_supernodes, _ = decode_url_features(shanghai_url)\n",
    "\n",
    "china_node = Supernode('China', features=shanghai_supernodes['China'] + shanghai_supernodes['China (2)'])#, \n",
    "\n",
    "for node in [china_node]:\n",
    "    dallas_austin_graph.initialize_node(node, shanghai_activations)\n",
    "\n",
    "china_interventions = [Intervention(texas_node, -2), Intervention(china_node, 2)]\n",
    "supernode_intervention(dallas_austin_graph, china_interventions, {texas_node.name: china_node})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This works as well! Beijing is now the models' most likely output.\n",
    "\n",
    "Does this always work? Let's try with the circuit for [`Fact: the capital of the territory containing Vancouver is` → `Victoria`](https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-fact-vancouver-victoria&clickedId=4_11742_9&pruningThreshold=0.45&pinnedIds=27_18221_10%2CE_32936_9%2C21_2236_10%2C19_15123_10%2C18_1025_10%2C14_12562_9%2C14_5600_9%2C6_11873_9%2C4_8439_9%2CE_6037_4%2CE_19412_7%2C4_11742_9%2C0_16137_7&supernodes=%5B%5B%22Canada%22%2C%2214_5600_9%22%2C%226_11873_9%22%2C%224_8439_9%22%5D%5D&densityThreshold=0.99&clerps=%5B%5B%2214_1412562_9%22%2C%22California%22%5D%2C%5B%224_411742_9%22%2C%22Pacific+northwest%22%5D%2C%5B%2221_2102236_10%22%2C%22Victoria%22%5D%2C%5B%2218_1801025_10%22%2C%22British+Columbia%22%5D%5D). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "vancouver_prompt = \"Fact: the capital of the territory containing Vancouver is\"\n",
    "_, vancouver_activations = model.get_activations(vancouver_prompt)\n",
    "\n",
    "vancouver_url = \"https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-fact-vancouver-victoria&clerps=%5B%5D&clickedId=4_11742_9&pruningThreshold=0.45&pinnedIds=27_18221_10%2CE_32936_9%2C21_2236_10%2C19_15123_10%2C18_1025_10%2C14_12562_9%2C14_5600_9%2C6_11873_9%2C4_8439_9%2CE_6037_4%2CE_19412_7%2C4_11742_9%2C0_16137_7&supernodes=%5B%5B%22Canada%22%2C%2214_5600_9%22%2C%226_11873_9%22%2C%224_8439_9%22%5D%5D\"\n",
    "vancouver_supernodes, _ = decode_url_features(vancouver_url)\n",
    "\n",
    "say_victoria_node = Supernode('Say Victoria', features=[Feature(layer=21, pos=10, feature_idx=2236)])\n",
    "bc_node = Supernode('British Columbia', features=[Feature(layer=18, pos=10, feature_idx=1025)], \n",
    "                       children=[say_victoria_node])\n",
    "\n",
    "for node in [say_victoria_node, bc_node]:\n",
    "    dallas_austin_graph.initialize_node(node, vancouver_activations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"700\" height=\"400\">\n",
       "    <!-- Background -->\n",
       "    <rect width=\"700\" height=\"400\" fill=\"#f5f5f5\"/>\n",
       "    <rect x=\"20\" y=\"20\" width=\"660\" height=\"360\" fill=\"white\" stroke=\"none\" rx=\"12\"/>\n",
       "    \n",
       "    <!-- Title -->\n",
       "    <text x=\"40\" y=\"45\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"14\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"1px\">Graph &amp; Interventions</text>\n",
       "    \n",
       "    <!-- Graph area (moved up significantly) -->\n",
       "    <g transform=\"translate(50, 0)\">\n",
       "        <line x1=\"450.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 383.8093677492609,157.9364805205158 379.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"150.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 220.952452655745,164.36493806727375 216.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"225.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"225.0,156.38888888888889 233.8093677492609,157.9364805205158 229.047547344255,164.36493806727375\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"211.94444444444446\" x2=\"375.0\" y2=\"156.38888888888889\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"375.0,156.38888888888889 370.952452655745,164.36493806727375 366.1906322507391,157.9364805205158\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"267.5\" x2=\"150.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"150.0,211.94444444444446 158.8093677492609,213.49203607607137 154.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"375.0\" y1=\"267.5\" x2=\"300.0\" y2=\"211.94444444444446\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,211.94444444444446 308.8093677492609,213.49203607607137 304.047547344255,219.92049362282933\" fill=\"#8B4513\"/>\n",
       "<line x1=\"225.0\" y1=\"156.38888888888889\" x2=\"300.0\" y2=\"100.83333333333331\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,100.83333333333331 295.952452655745,108.8093825117182 291.1906322507391,102.38092496496024\" fill=\"#8B4513\"/>\n",
       "<line x1=\"405.0\" y1=\"121.38888888888889\" x2=\"330.0\" y2=\"65.83333333333331\" stroke=\"#D2691E\" stroke-width=\"4\"/>\n",
       "<polygon points=\"330.0,65.83333333333331 338.8093677492609,67.38092496496024 334.047547344255,73.8093825117182\" fill=\"#D2691E\"/>\n",
       "        <rect x=\"175.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: capital</text>\n",
       "<rect x=\"325.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: state</text>\n",
       "<rect x=\"100.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"150.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">capital</text>\n",
       "<rect x=\"85.0\" y=\"189.44444444444446\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"100.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"250.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">state</text>\n",
       "<rect x=\"235.0\" y=\"189.44444444444446\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"201.44444444444446\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"400.0\" y=\"194.44444444444446\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"450.0\" y=\"216.44444444444446\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Emb: Dallas</text>\n",
       "<rect x=\"175.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"225.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say a capital</text>\n",
       "<rect x=\"160.0\" y=\"133.88888888888889\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"175.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">102%</text>\n",
       "<rect x=\"325.0\" y=\"138.88888888888889\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"375.0\" y=\"160.88888888888889\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Texas</text>\n",
       "<rect x=\"305.0\" y=\"133.88888888888889\" width=\"34\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"322.0\" y=\"145.88888888888889\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">-2x</text>\n",
       "<rect x=\"250.0\" y=\"83.33333333333331\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"105.33333333333331\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say Austin</text>\n",
       "<rect x=\"235.0\" y=\"78.33333333333331\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"90.33333333333331\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">25%</text>\n",
       "<rect x=\"280.0\" y=\"48.333333333333314\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"330.0\" y=\"70.33333333333331\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say Victoria</text>\n",
       "<rect x=\"265.0\" y=\"43.333333333333314\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"280.0\" y=\"55.333333333333314\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">0%</text>\n",
       "<rect x=\"355.0\" y=\"103.88888888888889\" width=\"100\" height=\"35\" fill=\"#FFF8DC\" stroke=\"#D2691E\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"405.0\" y=\"125.88888888888889\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">British Columbia</text>\n",
       "<rect x=\"335.0\" y=\"98.88888888888889\" width=\"26\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"348.0\" y=\"110.88888888888889\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">2x</text>\n",
       "    </g>\n",
       "    \n",
       "    <!-- Prompt section -->\n",
       "    <line x1=\"40\" y1=\"290\" x2=\"660\" y2=\"290\" stroke=\"#ddd\" stroke-width=\"1\"/>\n",
       "    <text x=\"40\" y=\"310\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Prompt</text>\n",
       "    \n",
       "    <!-- Prompt text (GitHub-compatible) -->\n",
       "    <text x=\"40\" y=\"325\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\">Fact: the capital of the state containing Dallas is</text>\n",
       "    \n",
       "    <!-- Top outputs section -->\n",
       "    <text x=\"40\" y=\"350\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Top Outputs</text>\n",
       "    \n",
       "    <!-- Output items -->\n",
       "    <g transform=\"translate(0, 5)\">\n",
       "        <rect x=\"40\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"45\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> the <tspan fill=\"#555\" font-size=\"10\">8%</tspan></text>\n",
       "<rect x=\"114\" y=\"350\" width=\"88\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"119\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Albany <tspan fill=\"#555\" font-size=\"10\">6%</tspan></text>\n",
       "<rect x=\"212\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"217\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> not <tspan fill=\"#555\" font-size=\"10\">6%</tspan></text>\n",
       "<rect x=\"286\" y=\"350\" width=\"120\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"291\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Harrisburg <tspan fill=\"#555\" font-size=\"10\">5%</tspan></text>\n",
       "<rect x=\"416\" y=\"350\" width=\"104\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"421\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> Hartford <tspan fill=\"#555\" font-size=\"10\">4%</tspan></text>\n",
       "    </g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bc_interventions = [Intervention(texas_node, -2), Intervention(bc_node, 2)]\n",
    "supernode_intervention(dallas_austin_graph, bc_interventions, {texas_node.name: bc_node, say_austin_node.name: say_victoria_node})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this case, the intervention was not very effective. The model's output looks like it did when just ablating Texas, which indicates that the addition of British Columbia did little. A motivated reader might want to try activating the British Columbia node more strongly - what happens?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Multilingual Circuits\n",
    "\n",
    "In this section, we'll look at multilingual circuits, as studied [in the original paper](https://transformer-circuits.pub/2025/attribution-graphs/biology.html#dives-multilingual). Specifically, we'll look at three circuits, for the same sentence in 3 languages:\n",
    "- English: [`The opposite of \"small\" is \"` → `big`](https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-small-big-en&pruningThreshold=0.65&pinnedIds=27_13210_8%2CE_10498_5%2C23_8683_8%2C21_10062_8%2C17_12530_5%2C18_9402_8%2C6_4362_5%2C15_5617_5%2C15_5756_5%2C19_5058_8%2C14_11360_5%2CE_13388_2%2C15_7209_2%2C4_95_2%2C3_6576_2%2C27_7773_8%2C7_10545_5&supernodes=%5B%5B%22Output+%5C%22big%5C%22+or+%5C%22large%5C%22%22%2C%2227_7773_8%22%2C%2227_13210_8%22%5D%2C%5B%22say+big+%2F+huge+%2F+large%22%2C%2221_10062_8%22%2C%2223_8683_8%22%5D%2C%5B%22opposite%22%2C%224_95_2%22%2C%2215_7209_2%22%2C%223_6576_2%22%5D%2C%5B%22small%22%2C%2214_11360_5%22%2C%2217_12530_5%22%2C%2215_5617_5%22%5D%2C%5B%22large+%2F+size%22%2C%226_4362_5%22%2C%227_10545_5%22%2C%2215_5756_5%22%5D%5D&clickedId=6_4362_5&densityThreshold=0.99&clerps=%5B%5B%2219_1905058_8%22%2C%22comparatives%22%5D%5D)\n",
    "- French: [`Le contraire de \"petit\" est \"` → `grand`](https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-small-big-fr&pruningThreshold=0.65&pinnedIds=27_21996_8%2CE_64986_5%2C24_16045_8%2C19_5058_8%2C21_10062_8%2C23_2592_8%2C20_1454_8%2CE_63265_2%2C23_8683_8%2C23_8488_8%2C20_11434_8%2C19_5802_8%2CE_1455_7%2C15_5617_5%2C18_9402_8%2C6_4362_5%2C14_11360_5%2C3_2908_5%2C2_5452_5%2C3_6627_5%2C6_16184_2%2C4_95_2%2C22_10566_8%2C21_1144_8%2CE_2025_1%2CE_581_3&supernodes=%5B%5B%22opposite%22%2C%226_16184_2%22%2C%224_95_2%22%5D%2C%5B%22say+big+%2F+large%22%2C%2223_8683_8%22%2C%2223_8488_8%22%2C%2221_10062_8%22%5D%2C%5B%22comparatives%22%2C%2219_5058_8%22%2C%2224_16045_8%22%2C%2220_11434_8%22%5D%2C%5B%22small%22%2C%2215_5617_5%22%2C%2214_11360_5%22%2C%223_6627_5%22%2C%223_2908_5%22%2C%222_5452_5%22%5D%2C%5B%22size%22%2C%2218_9402_8%22%2C%226_4362_5%22%5D%2C%5B%22French%22%2C%2221_1144_8%22%2C%2222_10566_8%22%2C%2220_1454_8%22%2C%2223_2592_8%22%2C%2219_5802_8%22%5D%5D&clickedId=22_10566_8&densityThreshold=0.99)\n",
    "- Chinese: [`\"小\"的反义词是\"` → `大`](http://neuronpedia.org/gemma-2-2b/graph?slug=gemma-small-big-zh&pruningThreshold=0.65&pinnedIds=27_235469_8%2CE_235585_2%2C23_8488_8%2C23_8683_8%2C21_10062_8%2C19_5058_8%2C22_11933_8%2C21_9377_8%2C18_9402_8%2C15_5617_2%2C14_11360_2%2C14_13476_2%2C2_2169_2%2C1_10169_2%2C8_1988_6%2C4_15846_6%2C4_7409_6%2CE_208659_4%2CE_237379_6%2CE_236711_5%2C24_2394_8%2C23_13630_8%2C21_13505_8%2C20_12983_8&supernodes=%5B%5B%22reverse%22%2C%224_7409_6%22%2C%228_1988_6%22%2C%224_15846_6%22%5D%2C%5B%22small%22%2C%2215_5617_2%22%2C%2214_11360_2%22%5D%2C%5B%22say+big+%2F+large%22%2C%2223_8683_8%22%2C%2221_10062_8%22%2C%2223_8488_8%22%5D%2C%5B%22Chinese%22%2C%2224_2394_8%22%2C%2222_11933_8%22%2C%2220_12983_8%22%2C%2221_13505_8%22%2C%2223_13630_8%22%5D%2C%5B%22Chinese-related+English+text%22%2C%221_10169_2%22%2C%2214_13476_2%22%5D%2C%5B%22size%22%2C%2218_9402_8%22%2C%222_2169_2%22%5D%2C%5B%22comparatives%22%2C%2221_9377_8%22%2C%2219_5058_8%22%5D%5D&clickedId=27_235469_8&densityThreshold=0.99)\n",
    "\n",
    "Work on Haiku showed one shared multilingual circuit: \n",
    "\n",
    "<img src=\"https://raw.githubusercontent.com/safety-research/circuit-tracer/main/demos/img/multilingual-haiku.png\" width=\"900\" />\n",
    "\n",
    "Our circuits indicate the same behavior. In fact, unlike the Haiku circuits, the Gemma 2 (2B) circuits are essentially entirely multilingual. There are no individual \"Say big\" or \"Say grand\" supernodes that cause the model to output a specific answer in a specific language. Instead, all circuits use \"Say big\" features, combined with a \"French\" or \"Chinese\" feature if the answer is non-English. \n",
    "\n",
    "Let's study these circuits by performing interventions on them. First, we'll create Supernode objects, as before:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "url_fr = 'https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-small-big-fr&clerps=%5B%5D&pruningThreshold=0.65&pinnedIds=27_21996_8%2CE_64986_5%2C24_16045_8%2C19_5058_8%2C21_10062_8%2C23_2592_8%2C20_1454_8%2CE_63265_2%2C23_8683_8%2C23_8488_8%2C20_11434_8%2C19_5802_8%2CE_1455_7%2C15_5617_5%2C18_9402_8%2C6_4362_5%2C14_11360_5%2C3_2908_5%2C2_5452_5%2C3_6627_5%2C6_16184_2%2C4_95_2%2C22_10566_8%2C21_1144_8%2CE_2025_1%2CE_581_3%2C5_982_2%2C6_651_2%2C5_8646_2%2C6_2743_2%2C8_1988_2%2C4_7409_2%2C4_15846_2%2C3_11241_2%2C2_321_2%2C2_4657_2&supernodes=%5B%5B%22say+big+%2F+large%22%2C%2223_8683_8%22%2C%2223_8488_8%22%2C%2221_10062_8%22%5D%2C%5B%22too%22%2C%2219_5058_8%22%2C%2224_16045_8%22%2C%2220_11434_8%22%5D%2C%5B%22small%22%2C%2215_5617_5%22%2C%2214_11360_5%22%2C%223_6627_5%22%2C%223_2908_5%22%2C%222_5452_5%22%5D%2C%5B%22size%22%2C%2218_9402_8%22%2C%226_4362_5%22%5D%2C%5B%22French%22%2C%2221_1144_8%22%2C%2222_10566_8%22%2C%2220_1454_8%22%2C%2223_2592_8%22%2C%2219_5802_8%22%5D%2C%5B%22opposite%22%2C%226_16184_2%22%2C%224_95_2%22%2C%225_8646_2%22%2C%226_2743_2%22%2C%228_1988_2%22%2C%226_651_2%22%2C%225_982_2%22%2C%222_321_2%22%2C%223_11241_2%22%2C%222_4657_2%22%2C%224_7409_2%22%2C%224_15846_2%22%5D%5D&clickedId=3_11241_2'\n",
    "supernodes_fr, _ = decode_url_features(url_fr)\n",
    "french_node = Supernode('French', features=supernodes_fr['French'], children=[])\n",
    "say_big_node = Supernode('Say big', features=supernodes_fr[\"say big / large\"])\n",
    "small_node = Supernode('small', features=supernodes_fr['small'], children=[say_big_node])\n",
    "opposite_node = Supernode('opposite', features=supernodes_fr['opposite'], children=[say_big_node])\n",
    "\n",
    "url_en = 'https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-small-big-en&clerps=%5B%5D&pruningThreshold=0.65&pinnedIds=27_13210_8%2CE_10498_5%2C23_8683_8%2C21_10062_8%2C17_12530_5%2C18_9402_8%2C6_4362_5%2C15_5617_5%2C15_5756_5%2C19_5058_8%2C14_11360_5%2CE_13388_2%2C15_7209_2%2C4_95_2%2C3_6576_2%2C27_7773_8%2C7_10545_5&supernodes=%5B%5B%22Output+%5C%22big%5C%22+or+%5C%22large%5C%22%22%2C%2227_7773_8%22%2C%2227_13210_8%22%5D%2C%5B%22say+big+%2F+huge+%2F+large%22%2C%2221_10062_8%22%2C%2223_8683_8%22%5D%2C%5B%22opposite%22%2C%224_95_2%22%2C%2215_7209_2%22%2C%223_6576_2%22%5D%2C%5B%22small%22%2C%2214_11360_5%22%2C%2217_12530_5%22%2C%2215_5617_5%22%5D%2C%5B%22large+%2F+size%22%2C%226_4362_5%22%2C%227_10545_5%22%2C%2215_5756_5%22%5D%5D&clickedId=6_4362_5'\n",
    "supernodes_en, _ = decode_url_features(url_en)\n",
    "\n",
    "\n",
    "url_zh = 'https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-small-big-zh&clerps=%5B%5D&pruningThreshold=0.65&pinnedIds=27_235469_8%2CE_235585_2%2C23_8488_8%2C23_8683_8%2C21_10062_8%2C19_5058_8%2C22_11933_8%2C21_9377_8%2C18_9402_8%2C15_5617_2%2C14_11360_2%2C14_13476_2%2C2_2169_2%2C1_10169_2%2C8_1988_6%2C4_15846_6%2C4_7409_6%2CE_208659_4%2CE_237379_6%2CE_236711_5%2C24_2394_8%2C23_13630_8%2C21_13505_8%2C20_12983_8&supernodes=%5B%5B%22reverse%22%2C%224_7409_6%22%2C%228_1988_6%22%2C%224_15846_6%22%5D%2C%5B%22small%22%2C%2215_5617_2%22%2C%2214_11360_2%22%5D%2C%5B%22say+big+%2F+large%22%2C%2223_8683_8%22%2C%2221_10062_8%22%2C%2223_8488_8%22%5D%2C%5B%22Chinese%22%2C%2224_2394_8%22%2C%2222_11933_8%22%2C%2220_12983_8%22%2C%2221_13505_8%22%2C%2223_13630_8%22%5D%2C%5B%22Chinese-related+English+text%22%2C%221_10169_2%22%2C%2214_13476_2%22%5D%2C%5B%22size%22%2C%2218_9402_8%22%2C%222_2169_2%22%5D%5D&clickedId=27_235469_8'\n",
    "supernodes_zh, _ = decode_url_features(url_zh)\n",
    "chinese_node = Supernode('Chinese', features=supernodes_zh['Chinese'] + supernodes_zh['Chinese-related English text'], children=[])\n",
    "\n",
    "ordered_nodes_fr = [[french_node, opposite_node, small_node], [say_big_node]]\n",
    "\n",
    "prompt_en = 'The opposite of \"small\" is \"'\n",
    "prompt_fr = 'Le contraire de \"petit\" est \"'\n",
    "prompt_zh = '\"小\"的反义词是\"'\n",
    "\n",
    "small_big_graph = InterventionGraph(ordered_nodes=ordered_nodes_fr, prompt=prompt_fr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then, we get the activations for these nodes, initialize them, and create a visualization."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"700\" height=\"400\">\n",
       "    <!-- Background -->\n",
       "    <rect width=\"700\" height=\"400\" fill=\"#f5f5f5\"/>\n",
       "    <rect x=\"20\" y=\"20\" width=\"660\" height=\"360\" fill=\"white\" stroke=\"none\" rx=\"12\"/>\n",
       "    \n",
       "    <!-- Title -->\n",
       "    <text x=\"40\" y=\"45\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"14\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"1px\">Graph &amp; Interventions</text>\n",
       "    \n",
       "    <!-- Graph area (moved up significantly) -->\n",
       "    <g transform=\"translate(50, 0)\">\n",
       "        <line x1=\"450.0\" y1=\"267.5\" x2=\"300.0\" y2=\"167.5\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,167.5 308.8752031396037,168.60940039245045 304.43760156980187,175.26580274715323\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"267.5\" x2=\"300.0\" y2=\"167.5\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,167.5 304.0,175.5 296.0,175.5\" fill=\"#8B4513\"/>\n",
       "        <rect x=\"100.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"150.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">French</text>\n",
       "<rect x=\"85.0\" y=\"245.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"100.0\" y=\"257.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"250.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">opposite</text>\n",
       "<rect x=\"235.0\" y=\"245.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"257.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"400.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"450.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">small</text>\n",
       "<rect x=\"385.0\" y=\"245.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"400.0\" y=\"257.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"250.0\" y=\"150.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"172.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say big</text>\n",
       "<rect x=\"235.0\" y=\"145.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"157.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "    </g>\n",
       "    \n",
       "    <!-- Prompt section -->\n",
       "    <line x1=\"40\" y1=\"290\" x2=\"660\" y2=\"290\" stroke=\"#ddd\" stroke-width=\"1\"/>\n",
       "    <text x=\"40\" y=\"310\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Prompt</text>\n",
       "    \n",
       "    <!-- Prompt text (GitHub-compatible) -->\n",
       "    <text x=\"40\" y=\"325\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\">Le contraire de &quot;petit&quot; est &quot;</text>\n",
       "    \n",
       "    <!-- Top outputs section -->\n",
       "    <text x=\"40\" y=\"350\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Top Outputs</text>\n",
       "    \n",
       "    <!-- Output items -->\n",
       "    <g transform=\"translate(0, 5)\">\n",
       "        <rect x=\"40\" y=\"350\" width=\"78\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"45\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">grand <tspan fill=\"#555\" font-size=\"10\">81%</tspan></text>\n",
       "<rect x=\"128\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"133\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">gros <tspan fill=\"#555\" font-size=\"10\">7%</tspan></text>\n",
       "<rect x=\"202\" y=\"350\" width=\"80\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"207\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> grand <tspan fill=\"#555\" font-size=\"10\">3%</tspan></text>\n",
       "<rect x=\"292\" y=\"350\" width=\"72\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"297\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">Grand <tspan fill=\"#555\" font-size=\"10\">1%</tspan></text>\n",
       "<rect x=\"374\" y=\"350\" width=\"80\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"379\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">grande <tspan fill=\"#555\" font-size=\"10\">1%</tspan></text>\n",
       "    </g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "logits_fr, activations_fr = model.get_activations(prompt_fr)\n",
    "\n",
    "for node in [say_big_node, small_node, french_node, opposite_node]:\n",
    "    small_big_graph.initialize_node(node, activations_fr)  # initialize each node, adding it to the graph and recording its default activation\n",
    "\n",
    "logits_zh, activations_zh = model.get_activations(prompt_zh)\n",
    "small_big_graph.initialize_node(chinese_node, activations_zh)\n",
    "\n",
    "small_big_graph.set_node_activation_fractions(activations_fr)  # set each node's current activation to a percent of its default activation (here always 100%)\n",
    "create_graph_visualization(small_big_graph, get_top_outputs(logits_fr))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Great! Now let's perform our first intervention: we'll turn off the French supernode."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"700\" height=\"400\">\n",
       "    <!-- Background -->\n",
       "    <rect width=\"700\" height=\"400\" fill=\"#f5f5f5\"/>\n",
       "    <rect x=\"20\" y=\"20\" width=\"660\" height=\"360\" fill=\"white\" stroke=\"none\" rx=\"12\"/>\n",
       "    \n",
       "    <!-- Title -->\n",
       "    <text x=\"40\" y=\"45\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"14\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"1px\">Graph &amp; Interventions</text>\n",
       "    \n",
       "    <!-- Graph area (moved up significantly) -->\n",
       "    <g transform=\"translate(50, 0)\">\n",
       "        <line x1=\"450.0\" y1=\"267.5\" x2=\"300.0\" y2=\"167.5\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,167.5 308.8752031396037,168.60940039245045 304.43760156980187,175.26580274715323\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"267.5\" x2=\"300.0\" y2=\"167.5\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,167.5 304.0,175.5 296.0,175.5\" fill=\"#8B4513\"/>\n",
       "        <rect x=\"100.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"150.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">French</text>\n",
       "<rect x=\"80.0\" y=\"245.0\" width=\"34\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"97.0\" y=\"257.0\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">-2x</text>\n",
       "<rect x=\"250.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">opposite</text>\n",
       "<rect x=\"235.0\" y=\"245.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"257.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"400.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"450.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">small</text>\n",
       "<rect x=\"385.0\" y=\"245.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"400.0\" y=\"257.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"250.0\" y=\"150.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"172.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say big</text>\n",
       "<rect x=\"235.0\" y=\"145.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"157.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">86%</text>\n",
       "    </g>\n",
       "    \n",
       "    <!-- Prompt section -->\n",
       "    <line x1=\"40\" y1=\"290\" x2=\"660\" y2=\"290\" stroke=\"#ddd\" stroke-width=\"1\"/>\n",
       "    <text x=\"40\" y=\"310\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Prompt</text>\n",
       "    \n",
       "    <!-- Prompt text (GitHub-compatible) -->\n",
       "    <text x=\"40\" y=\"325\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\">Le contraire de &quot;petit&quot; est &quot;</text>\n",
       "    \n",
       "    <!-- Top outputs section -->\n",
       "    <text x=\"40\" y=\"350\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Top Outputs</text>\n",
       "    \n",
       "    <!-- Output items -->\n",
       "    <g transform=\"translate(0, 5)\">\n",
       "        <rect x=\"40\" y=\"350\" width=\"62\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"45\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">big <tspan fill=\"#555\" font-size=\"10\">52%</tspan></text>\n",
       "<rect x=\"112\" y=\"350\" width=\"72\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"117\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">large <tspan fill=\"#555\" font-size=\"10\">9%</tspan></text>\n",
       "<rect x=\"194\" y=\"350\" width=\"72\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"199\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">great <tspan fill=\"#555\" font-size=\"10\">8%</tspan></text>\n",
       "<rect x=\"276\" y=\"350\" width=\"80\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"281\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">grande <tspan fill=\"#555\" font-size=\"10\">6%</tspan></text>\n",
       "<rect x=\"366\" y=\"350\" width=\"56\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"371\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">gig <tspan fill=\"#555\" font-size=\"10\">1%</tspan></text>\n",
       "    </g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "french_to_english_interventions = [Intervention(french_node, -2)]\n",
    "supernode_intervention(small_big_graph, french_to_english_interventions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Turning off the French supernode resulted in English output! Notably, it had only minor effects on the \"Say big\" supernode; their effects seem to be independent. \n",
    "\n",
    "Now, let's try to change the language to another: we'll turn off the French supernode, and turn on the Chinese supernode."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"700\" height=\"400\">\n",
       "    <!-- Background -->\n",
       "    <rect width=\"700\" height=\"400\" fill=\"#f5f5f5\"/>\n",
       "    <rect x=\"20\" y=\"20\" width=\"660\" height=\"360\" fill=\"white\" stroke=\"none\" rx=\"12\"/>\n",
       "    \n",
       "    <!-- Title -->\n",
       "    <text x=\"40\" y=\"45\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"14\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"1px\">Graph &amp; Interventions</text>\n",
       "    \n",
       "    <!-- Graph area (moved up significantly) -->\n",
       "    <g transform=\"translate(50, 0)\">\n",
       "        <line x1=\"450.0\" y1=\"267.5\" x2=\"300.0\" y2=\"167.5\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,167.5 308.8752031396037,168.60940039245045 304.43760156980187,175.26580274715323\" fill=\"#8B4513\"/>\n",
       "<line x1=\"300.0\" y1=\"267.5\" x2=\"300.0\" y2=\"167.5\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,167.5 304.0,175.5 296.0,175.5\" fill=\"#8B4513\"/>\n",
       "        <rect x=\"100.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"150.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">French</text>\n",
       "<rect x=\"80.0\" y=\"245.0\" width=\"34\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"97.0\" y=\"257.0\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">-2x</text>\n",
       "<rect x=\"250.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">opposite</text>\n",
       "<rect x=\"235.0\" y=\"245.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"257.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">99%</text>\n",
       "<rect x=\"400.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"450.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">small</text>\n",
       "<rect x=\"385.0\" y=\"245.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"400.0\" y=\"257.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"250.0\" y=\"150.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"172.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say big</text>\n",
       "<rect x=\"235.0\" y=\"145.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"157.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">88%</text>\n",
       "<rect x=\"130.0\" y=\"215.0\" width=\"100\" height=\"35\" fill=\"#FFF8DC\" stroke=\"#D2691E\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"180.0\" y=\"237.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Chinese</text>\n",
       "<rect x=\"110.0\" y=\"210.0\" width=\"26\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"123.0\" y=\"222.0\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">2x</text>\n",
       "    </g>\n",
       "    \n",
       "    <!-- Prompt section -->\n",
       "    <line x1=\"40\" y1=\"290\" x2=\"660\" y2=\"290\" stroke=\"#ddd\" stroke-width=\"1\"/>\n",
       "    <text x=\"40\" y=\"310\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Prompt</text>\n",
       "    \n",
       "    <!-- Prompt text (GitHub-compatible) -->\n",
       "    <text x=\"40\" y=\"325\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\">Le contraire de &quot;petit&quot; est &quot;</text>\n",
       "    \n",
       "    <!-- Top outputs section -->\n",
       "    <text x=\"40\" y=\"350\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Top Outputs</text>\n",
       "    \n",
       "    <!-- Output items -->\n",
       "    <g transform=\"translate(0, 5)\">\n",
       "        <rect x=\"40\" y=\"350\" width=\"46\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"45\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">大 <tspan fill=\"#555\" font-size=\"10\">85%</tspan></text>\n",
       "<rect x=\"96\" y=\"350\" width=\"48\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"101\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> 大 <tspan fill=\"#555\" font-size=\"10\">2%</tspan></text>\n",
       "<rect x=\"154\" y=\"350\" width=\"48\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"159\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">大小 <tspan fill=\"#555\" font-size=\"10\">1%</tspan></text>\n",
       "<rect x=\"212\" y=\"350\" width=\"48\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"217\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">大的 <tspan fill=\"#555\" font-size=\"10\">1%</tspan></text>\n",
       "<rect x=\"270\" y=\"350\" width=\"56\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"275\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">big <tspan fill=\"#555\" font-size=\"10\">1%</tspan></text>\n",
       "    </g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "french_to_chinese_interventions = [Intervention(french_node, -2), Intervention(chinese_node, 2)]\n",
    "supernode_intervention(small_big_graph, french_to_chinese_interventions, replacements={french_node.name: chinese_node})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As expected, the model's output post-intervention is identical to its original output on the Chinese example.\n",
    "\n",
    "What if we replace the \"small\" feature with a \"big\" feature?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"700\" height=\"400\">\n",
       "    <!-- Background -->\n",
       "    <rect width=\"700\" height=\"400\" fill=\"#f5f5f5\"/>\n",
       "    <rect x=\"20\" y=\"20\" width=\"660\" height=\"360\" fill=\"white\" stroke=\"none\" rx=\"12\"/>\n",
       "    \n",
       "    <!-- Title -->\n",
       "    <text x=\"40\" y=\"45\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"14\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"1px\">Graph &amp; Interventions</text>\n",
       "    \n",
       "    <!-- Graph area (moved up significantly) -->\n",
       "    <g transform=\"translate(50, 0)\">\n",
       "        <line x1=\"480.0\" y1=\"232.5\" x2=\"330.0\" y2=\"132.5\" stroke=\"#D2691E\" stroke-width=\"4\"/>\n",
       "<polygon points=\"330.0,132.5 338.8752031396037,133.60940039245045 334.43760156980187,140.26580274715323\" fill=\"#D2691E\"/>\n",
       "<line x1=\"300.0\" y1=\"267.5\" x2=\"300.0\" y2=\"167.5\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,167.5 304.0,175.5 296.0,175.5\" fill=\"#8B4513\"/>\n",
       "        <rect x=\"100.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"150.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">French</text>\n",
       "<rect x=\"85.0\" y=\"245.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"100.0\" y=\"257.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">93%</text>\n",
       "<rect x=\"250.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">opposite</text>\n",
       "<rect x=\"235.0\" y=\"245.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"257.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"400.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"450.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">small</text>\n",
       "<rect x=\"380.0\" y=\"245.0\" width=\"34\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"397.0\" y=\"257.0\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">-2x</text>\n",
       "<rect x=\"250.0\" y=\"150.0\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"172.0\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say big</text>\n",
       "<rect x=\"235.0\" y=\"145.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"157.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">0%</text>\n",
       "<rect x=\"430.0\" y=\"215.0\" width=\"100\" height=\"35\" fill=\"#FFF8DC\" stroke=\"#D2691E\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"480.0\" y=\"237.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">big</text>\n",
       "<rect x=\"410.0\" y=\"210.0\" width=\"26\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"423.0\" y=\"222.0\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">2x</text>\n",
       "<rect x=\"280.0\" y=\"115.0\" width=\"100\" height=\"35\" fill=\"#FFF8DC\" stroke=\"#D2691E\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"330.0\" y=\"137.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say small</text>\n",
       "<rect x=\"265.0\" y=\"110.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"280.0\" y=\"122.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">105%</text>\n",
       "    </g>\n",
       "    \n",
       "    <!-- Prompt section -->\n",
       "    <line x1=\"40\" y1=\"290\" x2=\"660\" y2=\"290\" stroke=\"#ddd\" stroke-width=\"1\"/>\n",
       "    <text x=\"40\" y=\"310\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Prompt</text>\n",
       "    \n",
       "    <!-- Prompt text (GitHub-compatible) -->\n",
       "    <text x=\"40\" y=\"325\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\">Le contraire de &quot;petit&quot; est &quot;</text>\n",
       "    \n",
       "    <!-- Top outputs section -->\n",
       "    <text x=\"40\" y=\"350\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Top Outputs</text>\n",
       "    \n",
       "    <!-- Output items -->\n",
       "    <g transform=\"translate(0, 5)\">\n",
       "        <rect x=\"40\" y=\"350\" width=\"78\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"45\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">petit <tspan fill=\"#555\" font-size=\"10\">87%</tspan></text>\n",
       "<rect x=\"128\" y=\"350\" width=\"80\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"133\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">petite <tspan fill=\"#555\" font-size=\"10\">3%</tspan></text>\n",
       "<rect x=\"218\" y=\"350\" width=\"56\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"223\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">&lt;b&gt; <tspan fill=\"#555\" font-size=\"10\">1%</tspan></text>\n",
       "<rect x=\"284\" y=\"350\" width=\"80\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"289\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> petit <tspan fill=\"#555\" font-size=\"10\">1%</tspan></text>\n",
       "<rect x=\"374\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"379\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">peti <tspan fill=\"#555\" font-size=\"10\">1%</tspan></text>\n",
       "    </g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url_fr_big_small = 'https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-big-small-fr&clerps=%5B%5D&pruningThreshold=0.65&clickedId=21_9082_8&pinnedIds=27_64986_8%2CE_21996_5%2C21_9082_8%2C15_5756_5%2C6_4362_5%2C3_2873_5%2C2_4298_5&supernodes=%5B%5B%22large+%2F+huge%22%2C%2215_5756_5%22%2C%226_4362_5%22%2C%223_2873_5%22%2C%222_4298_5%22%5D%5D'\n",
    "supernodes_fr_big_small, _ = decode_url_features(url_fr_big_small)\n",
    "say_small_node = Supernode('Say small', features=[Feature(layer=21, pos=8, feature_idx=9082)])\n",
    "big_node = Supernode('big', features=supernodes_fr_big_small['large / huge'], children=[say_small_node])\n",
    "\n",
    "prompt_fr_rev = 'Le contraire de \"grand\" est \"'\n",
    "logits_fr_rev, activations_fr_rev = model.get_activations(prompt_fr_rev)\n",
    "\n",
    "for node in [say_small_node, big_node]:\n",
    "    small_big_graph.initialize_node(node, activations_fr_rev)  # initialize each node, adding it to the graph and recording its default activation\n",
    "\n",
    "big_to_small_interventions = [Intervention(small_node, -2), Intervention(big_node, 2)]\n",
    "supernode_intervention(small_big_graph, big_to_small_interventions, replacements={small_node.name: big_node, say_big_node.name: say_small_node})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Replacing the \"small\" supernode with a \"big\" supernode causes the \"Say big\" supernode to shut off, and a new \"Say small\" supernode to turn on! The model's output changes to \"petit\", or \"small\", in French.\n",
    "\n",
    "We'll try one last intervention - can we replace the \"opposite\" supernode with a \"synonym\" supernode, to obtain a synonymous output? Our model is not very good at synonymy; given 'Un synonyme de \"petit\" est \"', it repeats \"petit\", rather than another synonym. But we can still see if this intervention reproduces that behavior."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Top outputs for Un synonyme de \"petit\" est \":  [('petit', 0.1826171875), ('petite', 0.11083984375), ('min', 0.09765625), ('jeune', 0.03173828125), ('ma', 0.0218505859375)]\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"700\" height=\"400\">\n",
       "    <!-- Background -->\n",
       "    <rect width=\"700\" height=\"400\" fill=\"#f5f5f5\"/>\n",
       "    <rect x=\"20\" y=\"20\" width=\"660\" height=\"360\" fill=\"white\" stroke=\"none\" rx=\"12\"/>\n",
       "    \n",
       "    <!-- Title -->\n",
       "    <text x=\"40\" y=\"45\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"14\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"1px\">Graph &amp; Interventions</text>\n",
       "    \n",
       "    <!-- Graph area (moved up significantly) -->\n",
       "    <g transform=\"translate(50, 0)\">\n",
       "        <line x1=\"450.0\" y1=\"267.5\" x2=\"300.0\" y2=\"167.5\" stroke=\"#8B4513\" stroke-width=\"3\"/>\n",
       "<polygon points=\"300.0,167.5 308.8752031396037,168.60940039245045 304.43760156980187,175.26580274715323\" fill=\"#8B4513\"/>\n",
       "        <rect x=\"100.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"150.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">French</text>\n",
       "<rect x=\"85.0\" y=\"245.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"100.0\" y=\"257.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">100%</text>\n",
       "<rect x=\"250.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#f0f0f0\" stroke=\"#ddd\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#bbb\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">opposite</text>\n",
       "<rect x=\"230.0\" y=\"245.0\" width=\"34\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"247.0\" y=\"257.0\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">-2x</text>\n",
       "<rect x=\"400.0\" y=\"250.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"450.0\" y=\"272.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">small</text>\n",
       "<rect x=\"385.0\" y=\"245.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"400.0\" y=\"257.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">102%</text>\n",
       "<rect x=\"250.0\" y=\"150.0\" width=\"100\" height=\"35\" fill=\"#e8e8e8\" stroke=\"#999\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"300.0\" y=\"172.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say big</text>\n",
       "<rect x=\"235.0\" y=\"145.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"250.0\" y=\"157.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">105%</text>\n",
       "<rect x=\"280.0\" y=\"215.0\" width=\"100\" height=\"35\" fill=\"#FFF8DC\" stroke=\"#D2691E\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"330.0\" y=\"237.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">synonym</text>\n",
       "<rect x=\"260.0\" y=\"210.0\" width=\"26\" height=\"16\" fill=\"#D2691E\" stroke=\"none\" rx=\"12\"/>\n",
       "<text x=\"273.0\" y=\"222.0\" text-anchor=\"middle\" fill=\"white\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">2x</text>\n",
       "<rect x=\"280.0\" y=\"115.0\" width=\"100\" height=\"35\" fill=\"#FFF8DC\" stroke=\"#D2691E\" stroke-width=\"2\" rx=\"8\"/>\n",
       "<text x=\"330.0\" y=\"137.0\" text-anchor=\"middle\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\">Say small</text>\n",
       "<rect x=\"265.0\" y=\"110.0\" width=\"30\" height=\"16\" fill=\"white\" stroke=\"#ccc\" stroke-width=\"1\" rx=\"4\"/>\n",
       "<text x=\"280.0\" y=\"122.0\" text-anchor=\"middle\" fill=\"#8B4513\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\">95%</text>\n",
       "    </g>\n",
       "    \n",
       "    <!-- Prompt section -->\n",
       "    <line x1=\"40\" y1=\"290\" x2=\"660\" y2=\"290\" stroke=\"#ddd\" stroke-width=\"1\"/>\n",
       "    <text x=\"40\" y=\"310\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"12\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Prompt</text>\n",
       "    \n",
       "    <!-- Prompt text (GitHub-compatible) -->\n",
       "    <text x=\"40\" y=\"325\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"12\">Le contraire de &quot;petit&quot; est &quot;</text>\n",
       "    \n",
       "    <!-- Top outputs section -->\n",
       "    <text x=\"40\" y=\"350\" fill=\"#666\" font-family=\"Arial, sans-serif\" font-size=\"10\" font-weight=\"bold\" text-transform=\"uppercase\" letter-spacing=\"0.5px\">Top Outputs</text>\n",
       "    \n",
       "    <!-- Output items -->\n",
       "    <g transform=\"translate(0, 5)\">\n",
       "        <rect x=\"40\" y=\"350\" width=\"78\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"45\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">grand <tspan fill=\"#555\" font-size=\"10\">76%</tspan></text>\n",
       "<rect x=\"128\" y=\"350\" width=\"64\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"133\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">gros <tspan fill=\"#555\" font-size=\"10\">7%</tspan></text>\n",
       "<rect x=\"202\" y=\"350\" width=\"80\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"207\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\"> grand <tspan fill=\"#555\" font-size=\"10\">3%</tspan></text>\n",
       "<rect x=\"292\" y=\"350\" width=\"72\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"297\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">petit <tspan fill=\"#555\" font-size=\"10\">2%</tspan></text>\n",
       "<rect x=\"374\" y=\"350\" width=\"80\" height=\"20\" fill=\"#e8e8e8\" stroke=\"none\" rx=\"6\"/>\n",
       "<text x=\"379\" y=\"364\" fill=\"#333\" font-family=\"Arial, sans-serif\" font-size=\"11\" font-weight=\"bold\">grande <tspan fill=\"#555\" font-size=\"10\">2%</tspan></text>\n",
       "    </g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url_fr_syn = 'https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-small-min-fr&clerps=%5B%5D&pruningThreshold=0.8&pinnedIds=27_64986_9%2C27_69658_9%2CE_64986_6%2C8_11850_3%2CE_14904_2%2C4_13762_3%2C6_10175_3%2C3_15891_3&supernodes=%5B%5B%22synonymy%22%2C%223_15891_3%22%2C%228_11850_3%22%2C%224_13762_3%22%2C%226_10175_3%22%5D%5D&clickedId=5_13985_3'\n",
    "supernodes_fr_syn, _ = decode_url_features(url_fr_syn)\n",
    "say_small_node2 = Supernode('say small', features=[Feature(layer=21, pos=8, feature_idx=9082)])\n",
    "synonym_node = Supernode('synonym', features=supernodes_fr_syn['synonymy'], children=[say_small_node2])\n",
    "\n",
    "prompt_fr_syn = 'Un synonyme de \"petit\" est \"'\n",
    "logits_fr_syn, activations_fr_syn = model.get_activations(prompt_fr_syn)\n",
    "print('Top outputs for Un synonyme de \"petit\" est \": ', get_top_outputs(logits_fr_syn))\n",
    "\n",
    "small_big_graph.initialize_node(synonym_node, activations_fr_syn)  # initialize each node, adding it to the graph and recording its default activation\n",
    "synonym_node.features = [Feature(layer=f.layer, pos=f.pos-1, feature_idx=f.feature_idx) for f in synonym_node.features]\n",
    "\n",
    "antonym_to_synonym_interventions = [Intervention(opposite_node, -2), Intervention(synonym_node, 2)]\n",
    "supernode_intervention(small_big_graph, antonym_to_synonym_interventions, replacements={opposite_node.name: synonym_node, say_big_node.name: say_small_node})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Unfortunately, this intervention doesn't work! Though the \"Say small\" supernode turns on, the \"Say big\" supernode stays on too, and the model's outputs don't change. This is not very surprising - if we look at the original [circuit](https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-small-big-fr&clerps=%5B%5D&pruningThreshold=0.65&pinnedIds=27_21996_8%2CE_64986_5%2C24_16045_8%2C19_5058_8%2C21_10062_8%2C23_2592_8%2C20_1454_8%2CE_63265_2%2C23_8683_8%2C23_8488_8%2C20_11434_8%2C19_5802_8%2CE_1455_7%2C15_5617_5%2C18_9402_8%2C6_4362_5%2C14_11360_5%2C3_2908_5%2C2_5452_5%2C3_6627_5%2C6_16184_2%2C4_95_2%2C22_10566_8%2C21_1144_8%2CE_2025_1%2CE_581_3&supernodes=%5B%5B%22opposite%22%2C%226_16184_2%22%2C%224_95_2%22%5D%2C%5B%22say+big+%2F+large%22%2C%2223_8683_8%22%2C%2223_8488_8%22%2C%2221_10062_8%22%5D%2C%5B%22too%22%2C%2219_5058_8%22%2C%2224_16045_8%22%2C%2220_11434_8%22%5D%2C%5B%22small%22%2C%2215_5617_5%22%2C%2214_11360_5%22%2C%223_6627_5%22%2C%223_2908_5%22%2C%222_5452_5%22%5D%2C%5B%22size%22%2C%2218_9402_8%22%2C%226_4362_5%22%5D%2C%5B%22French%22%2C%2221_1144_8%22%2C%2222_10566_8%22%2C%2220_1454_8%22%2C%2223_2592_8%22%2C%2219_5802_8%22%5D%5D&clickedId=22_10566_8) for the task, we see that the \"opposite\" supernode has only weak connections to the output. As a result, its causal effect is rather low, even though it would make sense for it to play a role."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Other Notebooks and (Un)Labeled Circuits\n",
    "\n",
    "Want to learn more? Check out the following other notebooks in the `demos` folder:\n",
    "These how to use our library:\n",
    "- `demos/attribute_demo.ipynb`: Demonstrates how to find circuits and visualize them. \n",
    "- `demos/intervention_demo.ipynb`: Demonstrates how to perform interventions on models. \n",
    "\n",
    "These dig deeper into specific, pre-computed and pre-annotated attribution graphs, performing interventions to demonstrate the correctness of the annotated graph:\n",
    "- `demos/gemma_demo.ipynb`: Explores graphs from Gemma 2 (2B)\n",
    "- `demos/gemma_it_demo.ipynb`: Explores graphs from instruction-tuned Gemma 2 (2B), using transcoders from the base model.\n",
    "- `demos/llama_demo.ipynb`: Explores graphs from Llama 3.2 (1B)\n",
    "\n",
    "You can also explore some more labeled examples:\n",
    "- [`The girls that the teacher sees` → `are`](https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-girls-are&pinnedIds=27_708_6%2C25_9974_6%2C22_11517_6%2CE_8216_2%2CE_674_3%2CE_651_1%2C19_1880_6%2C15_13979_6%2C17_7377_6%2C18_703_6%2C16_3689_6%2C15_4906_6%2C15_233_6%2CE_17733_6%2C3_6616_6%2C6_11265_6%2C5_1034_6%2C4_2671_6%2C3_6243_4%2C3_9864_3%2C0_13503_3&clickedId=3_9864_3&supernodes=%5B%5B%22see%2Fsaw%22%2C%2215_233_6%22%2C%226_11265_6%22%2C%223_6616_6%22%5D%2C%5B%22ends+of+noun+phrases+%28predict+a+verb%29%22%2C%2219_1880_6%22%2C%2217_7377_6%22%5D%2C%5B%22verbs+ending+relative+clauses%22%2C%224_2671_6%22%2C%2215_4906_6%22%2C%2215_13979_6%22%2C%2218_703_6%22%5D%2C%5B%22that%22%2C%220_13503_3%22%2C%223_9864_3%22%5D%5D&pruningThreshold=0.7&densityThreshold=0.99&clerps=%5B%5B%2225_2509974_6%22%2C%22say+are%22%5D%2C%5B%225_501034_6%22%2C%22transitive+verbs+with+objects+preceding+htem%22%5D%5D)\n",
    "- [`The International Advanced Security Group (IAS` → `G`](https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-G&pinnedIds=25_5604_7%2C24_763_7%2C22_12304_7%2C23_14585_7%2C24_1668_7%2C20_7544_7%2C17_4855_5%2C24_9503_7%2C17_4886_5%2C14_1031_5%2C13_7451_5%2C4_3134_5%2CE_5897_5%2C1_3977_5%2C13_5661_7%2C11_10532_7%2C11_7419_7%2C10_4451_7%2C6_9719_7%2CE_24632_7%2CE_591_6%2C0_548_7%2C2_12787_7%2C2_12811_7%2C2_4716_7%2C2_8870_7%2C5_10381_7%2C6_3358_7%2C7_7303_7%2C7_4807_7%2C7_15088_7%2C8_4119_7%2C10_2379_7%2C9_15938_7%2C10_8308_7%2C10_11210_7%2C14_922_7%2C15_5076_7%2C14_15510_7%2C15_444_7%2C17_14853_7%2C27_235319_7%2C2_8493_5%2C3_4791_6%2C4_6672_6%2C5_12154_6%2C5_112_6%2C8_15626_7%2C13_945_6%2C9_13890_6%2C5_12910_7%2C14_1031_6%2C14_13599_6%2C21_5066_6%2C13_13476_6%2C16_3033_6%2C25_4062_7%2C15_1301_6%2C6_11788_7%2C7_12830_7%2C11_8369_6%2C0_14394_7%2C0_7197_7%2C0_4370_7%2C0_3410_7%2C0_5548_7%2C0_13190_7%2C0_2592_7%2C1_10188_7%2C15_12642_5%2C23_999_7%2C14_13969_5%2C21_1146_7%2C24_2871_7%2C25_5880_7%2C24_7620_7%2C23_12120_7%2C25_10087_7&supernodes=%5B%5B%22Activates+on+%28+before+acronym+%2F+upweights+G*%22%2C%2220_7544_7%22%2C%2222_12304_7%22%5D%2C%5B%22%5C%22group%5C%22%22%2C%221_3977_5%22%2C%222_8493_5%22%2C%2217_4855_5%22%5D%2C%5B%22Activates+on+%28+before+acronym%22%2C%2221_5066_6%22%2C%2211_8369_6%22%2C%2213_945_6%22%2C%229_13890_6%22%2C%2214_13599_6%22%2C%2215_1301_6%22%2C%2216_3033_6%22%2C%2213_13476_6%22%2C%225_112_6%22%2C%225_12154_6%22%2C%224_6672_6%22%2C%223_4791_6%22%5D%2C%5B%22Predict+tokens+starting+with+G%22%2C%2223_999_7%22%2C%2224_1668_7%22%2C%2225_5604_7%22%5D%2C%5B%22Tokens+in+acronymable+proper+nouns%22%2C%2213_7451_5%22%2C%2214_13969_5%22%5D%2C%5B%22tokens+followed+by+G%22%2C%2221_1146_7%22%2C%2224_2871_7%22%2C%2225_5880_7%22%2C%2224_7620_7%22%5D%2C%5B%22all+caps%22%2C%2225_10087_7%22%2C%2223_12120_7%22%2C%2223_14585_7%22%2C%2224_763_7%22%5D%2C%5B%22Tokens+starting+with+G%22%2C%2214_1031_6%22%2C%2215_12642_5%22%2C%2214_1031_5%22%2C%224_3134_5%22%2C%2217_4886_5%22%5D%2C%5B%22first+token+of+acronyms%22%2C%220_7197_7%22%2C%220_5548_7%22%2C%220_3410_7%22%2C%220_4370_7%22%2C%222_8870_7%22%2C%220_13190_7%22%2C%220_14394_7%22%2C%220_2592_7%22%2C%221_10188_7%22%2C%227_12830_7%22%2C%2213_5661_7%22%2C%2214_922_7%22%2C%2217_14853_7%22%2C%227_15088_7%22%2C%229_15938_7%22%2C%2210_8308_7%22%2C%2215_5076_7%22%2C%2210_11210_7%22%2C%2211_10532_7%22%2C%226_9719_7%22%2C%225_10381_7%22%2C%222_12811_7%22%2C%222_4716_7%22%5D%2C%5B%22tokens+of+acronyms+in+parentheses%22%2C%2215_444_7%22%2C%228_4119_7%22%2C%2210_2379_7%22%2C%226_3358_7%22%2C%225_12910_7%22%2C%2224_9503_7%22%2C%226_11788_7%22%2C%2225_4062_7%22%2C%220_548_7%22%2C%2211_7419_7%22%2C%2210_4451_7%22%2C%2214_15510_7%22%2C%227_7303_7%22%2C%227_4807_7%22%2C%222_12787_7%22%2C%228_15626_7%22%5D%5D&clickedId=24_13541_7&pruningThreshold=0.7&densityThreshold=0.99)\n",
    "- [`The guitarist knew the song` → `. / is`](https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-gp-nps&clerps=%5B%5B%222413277%22%2C%22%28incomprehensible%29%22%5D%2C%5B%222106697%22%2C%22The+X+at+start+of+sentence+%28subject+NPs%29%22%5D%2C%5B%222012754%22%2C%22ends+of+NPs%2C+upweights+verbs%22%5D%2C%5B%222305096%22%2C%22ends+of+phrases+%22%5D%2C%5B%222001993%22%2C%22say+%5C%22would%5C%22%22%5D%2C%5B%222111913%22%2C%22subject+in+sentential+clause+%28say+%5C%22had%5C%22%29%22%5D%2C%5B%222012650%22%2C%22say+a+verb%22%5D%2C%5B%221702296%22%2C%22thought+%28say+a+verb%29%22%5D%2C%5B%221609179%22%2C%22knew%22%5D%2C%5B%221401162%22%2C%22was%2Fwere%22%5D%2C%5B%221303459%22%2C%22knew%2Fknow%22%5D%2C%5B%222310652%22%2C%22subject+in+sentential+clause+%28say+%5C%22would%5C%22%29%22%5D%2C%5B%222514276%22%2C%22don%27t+say+%5C%22well%5C%22%22%5D%2C%5B%222206853%22%2C%22don%27t+say+%5C%22well%5C%22%22%5D%2C%5B%221915834%22%2C%22say+%5C%22well%5C%22%22%5D%2C%5B%221409346%22%2C%22know%22%5D%2C%5B%222301993%22%2C%22say+%5C%22was%5C%22%22%5D%2C%5B%222207306%22%2C%22say+%5C%22was%5C%22%22%5D%2C%5B%222108443%22%2C%22say+%5C%22well%5C%22%22%5D%2C%5B%22701641%22%2C%22know+%2F+understand%22%5D%2C%5B%22600576%22%2C%22know+%28that%29%22%5D%2C%5B%221804181%22%2C%22sentential+verbs+%28say+a+verb%29%22%5D%2C%5B%22205370%22%2C%22want%22%5D%2C%5B%22307146%22%2C%22know%22%5D%2C%5B%22414214%22%2C%22know%22%5D%2C%5B%222105739%22%2C%22ends+of+phrases%22%5D%2C%5B%221914505%22%2C%22%5C%22song%5C%22%22%5D%2C%5B%221500908%22%2C%22know%22%5D%2C%5B%22600908%22%2C%22know%22%5D%2C%5B%222301612%22%2C%22say+a+verb%22%5D%2C%5B%222101806%22%2C%22say+a+verb%22%5D%2C%5B%221204795%22%2C%22know%22%5D%2C%5B%2221_2111913_5%22%2C%22sentential+subjects+%28say+a+verb%29%22%5D%5D&pinnedIds=27_729_5%2CE_6608_3%2C21_11913_5%2C23_1993_5%2C20_12650_5%2C22_7306_5%2CE_91939_2%2CE_5169_5%2C17_2296_3%2C18_4181_5%2C14_9346_3%2C20_1993_5%2C19_14505_5%2C16_9179_3%2C15_908_3%2C14_9346_4%2C6_576_3%2C6_908_3%2C12_4795_3%2C13_3459_3%2C7_1641_3%2C4_14214_3%2C23_1612_5%2C21_1806_5&supernodes=%5B%5B%22say+a+verb%22%2C%2220_12650_5%22%2C%2221_1806_5%22%2C%2223_1612_5%22%5D%2C%5B%22say+%5C%22was%5C%22%22%2C%2223_1993_5%22%2C%2222_7306_5%22%5D%2C%5B%22knew%22%2C%2216_9179_3%22%2C%224_14214_3%22%2C%2213_3459_3%22%2C%2212_4795_3%22%2C%2214_9346_3%22%2C%2215_908_3%22%2C%227_1641_3%22%2C%226_576_3%22%2C%226_908_3%22%2C%2214_9346_4%22%5D%2C%5B%22ends+of+phrases%22%2C%2221_5739_5%22%2C%2223_5096_5%22%5D%2C%5B%22ends+of+NPs%2C+upweights+verbs%22%2C%2220_12754_5%22%2C%2221_6697_5%22%5D%2C%5B%22sentential+verbs+%28say+a+verb%29%22%2C%2221_11913_5%22%2C%2218_4181_5%22%2C%2217_2296_3%22%5D%5D&pruningThreshold=0.51&densityThreshold=0.99)\n",
    "\n",
    "Or try to find circuits in unlabeled ones!:\n",
    "- [`The language most commonly spoken in the country south of the United States is` → `Spanish`](https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-Mexico-Spanish&clickedId=undefined&pruningThreshold=0.7&densityThreshold=0.99)\n",
    "- [`A bee is a type of` → `insect`](https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-bee-insect&clerps=%5B%5D&clickedId=undefined)\n",
    "- [`cat, bat, hat` → `rat`](https://www.neuronpedia.org/gemma-2-2b/graph?slug=gemma-cat-hat&clerps=%5B%5D&clickedId=undefined)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<head>\n",
    "    <style>\n",
    "        body {\n",
    "            margin: 20px;\n",
    "            font-family: Arial, sans-serif;\n",
    "            background-color: #f5f5f5;\n",
    "        }\n",
    "        \n",
    "        .container {\n",
    "            background: white;\n",
    "            border-radius: 12px;\n",
    "            padding: 20px;\n",
    "            box-shadow: 0 2px 10px rgba(0,0,0,0.1);\n",
    "            max-width: 900px;\n",
    "            margin: 0 auto;\n",
    "        }\n",
    "        \n",
    "        .title {\n",
    "            font-size: 18px;\n",
    "            font-weight: bold;\n",
    "            color: #666;\n",
    "            margin-bottom: 20px;\n",
    "            text-transform: uppercase;\n",
    "            letter-spacing: 1px;\n",
    "        }\n",
    "        \n",
    "        .graph-container {\n",
    "            position: relative;\n",
    "            height: 400px;\n",
    "            margin: 20px 0;\n",
    "            overflow: visible;\n",
    "        }\n",
    "        \n",
    "        .node {\n",
    "            position: absolute;\n",
    "            background: #e8e8e8;\n",
    "            border: 2px solid #999;\n",
    "            border-radius: 8px;\n",
    "            padding: 12px 16px;\n",
    "            font-weight: bold;\n",
    "            color: #333;\n",
    "            text-align: center;\n",
    "            cursor: pointer;\n",
    "            transition: all 0.3s ease;\n",
    "            min-width: 80px;\n",
    "            box-sizing: border-box;\n",
    "        }\n",
    "        \n",
    "        .node.low-activation {\n",
    "            background: #f0f0f0;\n",
    "            color: #bbb;\n",
    "            border-color: #ddd;\n",
    "        }\n",
    "        \n",
    "        .node.replacement {\n",
    "            border: 2px solid #D2691E;\n",
    "            background: #FFF8DC;\n",
    "        }\n",
    "        \n",
    "        .node:hover {\n",
    "            transform: translateY(-2px);\n",
    "            box-shadow: 0 4px 12px rgba(0,0,0,0.2);\n",
    "        }\n",
    "        \n",
    "        .activation-label {\n",
    "            position: absolute;\n",
    "            font-size: 12px;\n",
    "            font-weight: bold;\n",
    "            color: #8B4513;\n",
    "            background: white;\n",
    "            padding: 2px 6px;\n",
    "            border-radius: 4px;\n",
    "            border: 1px solid #ccc;\n",
    "            white-space: nowrap;\n",
    "        }\n",
    "        \n",
    "        .intervention {\n",
    "            position: absolute;\n",
    "            background: #D2691E;\n",
    "            color: white;\n",
    "            border-radius: 12px;\n",
    "            padding: 4px 12px;\n",
    "            font-size: 12px;\n",
    "            font-weight: bold;\n",
    "            white-space: nowrap;\n",
    "        }\n",
    "        \n",
    "        .connection {\n",
    "            position: absolute;\n",
    "            background: #8B4513;\n",
    "            height: 3px;\n",
    "            transform-origin: left center;\n",
    "        }\n",
    "        \n",
    "        .connection.replacement {\n",
    "            background: #D2691E;\n",
    "            height: 4px;\n",
    "        }\n",
    "        \n",
    "        .arrow {\n",
    "            position: absolute;\n",
    "            width: 0;\n",
    "            height: 0;\n",
    "            border-left: 8px solid #8B4513;\n",
    "            border-top: 5px solid transparent;\n",
    "            border-bottom: 5px solid transparent;\n",
    "        }\n",
    "        \n",
    "        .arrow.replacement {\n",
    "            border-left-color: #D2691E;\n",
    "        }\n",
    "        \n",
    "        .prompt-section {\n",
    "            margin-top: 30px;\n",
    "            padding-top: 20px;\n",
    "            border-top: 1px solid #ddd;\n",
    "        }\n",
    "        \n",
    "        .prompt-title {\n",
    "            font-size: 16px;\n",
    "            font-weight: bold;\n",
    "            color: #666;\n",
    "            margin-bottom: 10px;\n",
    "            text-transform: uppercase;\n",
    "            letter-spacing: 0.5px;\n",
    "        }\n",
    "        \n",
    "        .prompt-text {\n",
    "            font-size: 16px;\n",
    "            line-height: 1.5;\n",
    "            margin-bottom: 20px;\n",
    "        }\n",
    "        \n",
    "        .outputs-section {\n",
    "            margin-top: 20px;\n",
    "        }\n",
    "        \n",
    "        .outputs-title {\n",
    "            font-size: 14px;\n",
    "            font-weight: bold;\n",
    "            color: #666;\n",
    "            margin-bottom: 15px;\n",
    "            text-transform: uppercase;\n",
    "            letter-spacing: 0.5px;\n",
    "        }\n",
    "        \n",
    "        .output-items {\n",
    "            display: flex;\n",
    "            flex-wrap: wrap;\n",
    "            gap: 10px;\n",
    "        }\n",
    "        \n",
    "        .output-item {\n",
    "            background: #e8e8e8;\n",
    "            padding: 6px 12px;\n",
    "            border-radius: 6px;\n",
    "            font-weight: bold;\n",
    "            display: flex;\n",
    "            align-items: center;\n",
    "            gap: 8px;\n",
    "        }\n",
    "        \n",
    "        .output-percentage {\n",
    "            font-size: 12px;\n",
    "            color: #666;\n",
    "        }\n",
    "    </style>\n",
    "</head>\n",
    "<body>\n",
    "    <div class=\"container\">\n",
    "        <div class=\"title\">Graph & Interventions</div>\n",
    "        \n",
    "        <div class=\"graph-container\" id=\"graphContainer\">\n",
    "            <!-- Supernodes and connections will be positioned here -->\n",
    "        </div>\n",
    "        \n",
    "        <div class=\"prompt-section\">\n",
    "            <div class=\"prompt-title\">Prompt</div>\n",
    "            <div class=\"prompt-text\" id=\"promptText\">\n",
    "                <!-- Prompt will be inserted here -->\n",
    "            </div>\n",
    "            \n",
    "            <div class=\"outputs-section\">\n",
    "                <div class=\"outputs-title\">Top Outputs</div>\n",
    "                <div class=\"output-items\" id=\"outputItems\">\n",
    "                    <!-- Output items will be inserted here -->\n",
    "                </div>\n",
    "            </div>\n",
    "        </div>\n",
    "    </div>\n",
    "\n",
    "    <script>\n",
    "        // Configuration - can be modified\n",
    "        const prompt = \"Fact: the capital of the state containing Dallas is\";\n",
    "        const top_outputs = [[\"Sacramento\", 0.97], [\"\", 0.007], [\"not\", 0.004], [\"the\", 0.003], [\"⏎\", 0.003], [\"()\", 0.002]];\n",
    "        \n",
    "        // Supernode layout configuration (base nodes only - replacements will be positioned automatically)\n",
    "        const nodes = [\n",
    "            ['capital', 'state', 'Dallas'],\n",
    "            ['Say a capital', 'Texas'], \n",
    "            ['Say Austin']\n",
    "        ];\n",
    "        \n",
    "        // Supernode properties (without x,y coordinates)\n",
    "        const nodeProperties = {\n",
    "            'capital': { activation: 1.0 },\n",
    "            'state': { activation: 1.0 },\n",
    "            'Dallas': { activation: 1.0 },\n",
    "            'Say a capital': { activation: 0.91 },\n",
    "            'Texas': { activation: null, intervention: '-2x', replacement_node: 'California' },\n",
    "            'Say Austin': { activation: 0.0, replacement_node: 'Say Sacramento' },\n",
    "            'California': { activation: null, intervention: '+2x', children: ['Say Sacramento'] },\n",
    "            'Say Sacramento': { activation: null }\n",
    "        };\n",
    "\n",
    "        // Connection data - includes both original and replacement connections\n",
    "        const connections = [\n",
    "            { from: 'capital', to: 'Say a capital' },\n",
    "            { from: 'state', to: 'Say a capital' },\n",
    "            { from: 'state', to: 'Texas' },\n",
    "            { from: 'Dallas', to: 'Texas' },\n",
    "            { from: 'Say a capital', to: 'Say Austin' },\n",
    "            { from: 'Texas', to: 'Say Austin', replacement: true }, // This gets replaced\n",
    "            { from: 'California', to: 'Say Sacramento', replacement: true }\n",
    "        ];\n",
    "\n",
    "        // Auto-calculate positions based on node layout\n",
    "        function calculateSupernodePositions() {\n",
    "            const containerWidth = 800;\n",
    "            const containerHeight = 350;\n",
    "            const nodeWidth = 120;\n",
    "            const nodeHeight = 40;\n",
    "            \n",
    "            const nodeData = {};\n",
    "            \n",
    "            // First, position the base nodes from the layout\n",
    "            for (let rowIndex = 0; rowIndex < nodes.length; rowIndex++) {\n",
    "                const row = nodes[rowIndex];\n",
    "                const rowY = containerHeight - (rowIndex * (containerHeight / (nodes.length + 0.5)));\n",
    "                \n",
    "                for (let colIndex = 0; colIndex < row.length; colIndex++) {\n",
    "                    const nodeName = row[colIndex];\n",
    "                    const rowWidth = row.length * nodeWidth + (row.length - 1) * 50;\n",
    "                    const startX = (containerWidth - rowWidth) / 2;\n",
    "                    const nodeX = startX + colIndex * (nodeWidth + 50);\n",
    "                    \n",
    "                    nodeData[nodeName] = {\n",
    "                        x: nodeX,\n",
    "                        y: rowY,\n",
    "                        ...nodeProperties[nodeName]\n",
    "                    };\n",
    "                }\n",
    "            }\n",
    "            \n",
    "            // Then, position replacement nodes directly above their original nodes (with slight offset)\n",
    "            Object.keys(nodeProperties).forEach(nodeName => {\n",
    "                const nodeProps = nodeProperties[nodeName];\n",
    "                if (nodeProps.replacement_node && !nodeData[nodeProps.replacement_node]) {\n",
    "                    const originalSupernode = nodeData[nodeName];\n",
    "                    if (originalSupernode) {\n",
    "                        // Position replacement node directly above with small right offset, touching the original\n",
    "                        nodeData[nodeProps.replacement_node] = {\n",
    "                            x: originalSupernode.x + 30,\n",
    "                            y: originalSupernode.y - 35,\n",
    "                            ...nodeProperties[nodeProps.replacement_node]\n",
    "                        };\n",
    "                    }\n",
    "                }\n",
    "            });\n",
    "            \n",
    "            return nodeData;\n",
    "        }\n",
    "        \n",
    "        // Calculate positions\n",
    "        const nodeData = calculateSupernodePositions();\n",
    "\n",
    "        function getSupernodeCenter(nodeName) {\n",
    "            const node = nodeData[nodeName];\n",
    "            if (!node) return { x: 0, y: 0 };\n",
    "            return {\n",
    "                x: node.x + 60, // Approximate center of node\n",
    "                y: node.y + 20\n",
    "            };\n",
    "        }\n",
    "\n",
    "        function createConnections() {\n",
    "            const container = document.getElementById('graphContainer');\n",
    "            \n",
    "            connections.forEach(conn => {\n",
    "                // Skip connections where the 'from' node has a replacement and this isn't a replacement connection\n",
    "                const fromSupernodeProps = nodeProperties[conn.from];\n",
    "                if (fromSupernodeProps && fromSupernodeProps.replacement_node && !conn.replacement) {\n",
    "                    return; // Skip this connection as it's replaced\n",
    "                }\n",
    "                \n",
    "                const fromCenter = getSupernodeCenter(conn.from);\n",
    "                const toCenter = getSupernodeCenter(conn.to);\n",
    "                \n",
    "                if (fromCenter.x === 0 || toCenter.x === 0) return; // Skip if node doesn't exist\n",
    "                \n",
    "                const dx = toCenter.x - fromCenter.x;\n",
    "                const dy = toCenter.y - fromCenter.y;\n",
    "                const length = Math.sqrt(dx * dx + dy * dy);\n",
    "                const angle = Math.atan2(dy, dx) * 180 / Math.PI;\n",
    "                \n",
    "                // Create connection line\n",
    "                const line = document.createElement('div');\n",
    "                line.className = conn.replacement ? 'connection replacement' : 'connection';\n",
    "                line.style.left = fromCenter.x + 'px';\n",
    "                line.style.top = fromCenter.y + 'px';\n",
    "                line.style.width = length + 'px';\n",
    "                line.style.transform = `rotate(${angle}deg)`;\n",
    "                container.appendChild(line);\n",
    "                \n",
    "                // Create arrow at the end of the line\n",
    "                const arrowX = toCenter.x - 8;\n",
    "                const arrowY = toCenter.y - 5;\n",
    "                \n",
    "                const arrow = document.createElement('div');\n",
    "                arrow.className = conn.replacement ? 'arrow replacement' : 'arrow';\n",
    "                arrow.style.left = arrowX + 'px';\n",
    "                arrow.style.top = arrowY + 'px';\n",
    "                container.appendChild(arrow);\n",
    "            });\n",
    "        }\n",
    "\n",
    "        function createSupernodes() {\n",
    "            const container = document.getElementById('graphContainer');\n",
    "\n",
    "            // Create nodes\n",
    "            Object.entries(nodeData).forEach(([name, data]) => {\n",
    "                const node = document.createElement('div');\n",
    "                node.className = 'node';\n",
    "                \n",
    "                // Check if node should be grayed out (low activation or negative intervention)\n",
    "                const isLowActivation = data.activation !== null && data.activation <= 0.25;\n",
    "                const hasNegativeIntervention = data.intervention && data.intervention.includes('-');\n",
    "                \n",
    "                if (isLowActivation || hasNegativeIntervention) {\n",
    "                    node.classList.add('low-activation');\n",
    "                }\n",
    "                \n",
    "                // Check if this is a replacement node\n",
    "                const isReplacement = Object.values(nodeProperties).some(props => \n",
    "                    props.replacement_node === name\n",
    "                );\n",
    "                if (isReplacement) {\n",
    "                    node.classList.add('replacement');\n",
    "                }\n",
    "                \n",
    "                node.textContent = name;\n",
    "                node.style.left = data.x + 'px';\n",
    "                node.style.top = data.y + 'px';\n",
    "                container.appendChild(node);\n",
    "\n",
    "                // Add activation label if exists (top-left of node)\n",
    "                if (data.activation !== null) {\n",
    "                    const label = document.createElement('div');\n",
    "                    label.className = 'activation-label';\n",
    "                    label.textContent = Math.round(data.activation * 100) + '%';\n",
    "                    \n",
    "                    label.style.left = (data.x - 15) + 'px';\n",
    "                    label.style.top = (data.y - 10) + 'px';\n",
    "                    container.appendChild(label);\n",
    "                }\n",
    "\n",
    "                // Add intervention on top-left of node if exists\n",
    "                if (data.intervention) {\n",
    "                    const intervention = document.createElement('div');\n",
    "                    intervention.className = 'intervention';\n",
    "                    intervention.textContent = data.intervention;\n",
    "                    intervention.style.left = (data.x - 20) + 'px';\n",
    "                    intervention.style.top = (data.y - 10) + 'px';\n",
    "                    container.appendChild(intervention);\n",
    "                }\n",
    "            });\n",
    "        }\n",
    "\n",
    "        function createPromptSection() {\n",
    "            // Set prompt text\n",
    "            document.getElementById('promptText').innerHTML = `<strong>Fact:</strong> ${prompt.replace('Fact: ', '')}`;\n",
    "            \n",
    "            // Create output items\n",
    "            const outputContainer = document.getElementById('outputItems');\n",
    "            top_outputs.forEach(([text, percentage]) => {\n",
    "                const item = document.createElement('div');\n",
    "                item.className = 'output-item';\n",
    "                \n",
    "                if (text === '') {\n",
    "                    item.innerHTML = `<span class=\"output-percentage\">${Math.round(percentage * 100)}%</span>`;\n",
    "                } else {\n",
    "                    item.innerHTML = `${text} <span class=\"output-percentage\">${Math.round(percentage * 100)}%</span>`;\n",
    "                }\n",
    "                \n",
    "                outputContainer.appendChild(item);\n",
    "            });\n",
    "        }\n",
    "\n",
    "        // Initialize the graph\n",
    "        createConnections();\n",
    "        createSupernodes();\n",
    "        createPromptSection();\n",
    "    </script>\n",
    "</body>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
